1

フォームを使用して pdf ファイル内のフィールドを編集するためのシステムを作成しようとしています。データを編集した後、バージョンを保存したいと考えています。ただし、必要条件は、多かれ少なかれ、フォームを使用する任意の pdf でシステムを使用することです。これに対する私の解決策は、iTextSharpを使用してpdfのフィールドを読み取り、モデル内の辞書に保存することです。(エンティティ フレームワークを使用して) データベース内にディクショナリを格納できるようにするために、文字列との間でディクショナリを結合/分割するシリアライザを作成しました。

問題は次のとおりです。フォームを保存するときに問題が発生します。データは問題なくビューに到着しますが、ビューから出てコントローラーに再び入ると、ディクショナリから作成されたフィールドからのデータはありません。問題は、辞書の使用とフォームの動的構築の両方から発生していると想定していますが、これをどのように解決するかがわかりません。

コードは次のとおりです。

モデル:

    public class Diplom
    {
        public string filnavn { get; set; }
        public int Id { get; set; }

        private Dictionary<string, string> _fields;

        private DateTime datevalue;


        // CTOR:
        public Diplom()
        {
            _fields = new Dictionary<string, string>();
        }

        public DateTime BestaattTid
        {
            get
            {
                return datevalue.Date;
            }
            set
            {
                datevalue = value;
            }
        }

        // Get/set, whole dictionary.
        public Dictionary<string, string> Fields
        {
            get
            {
                return _fields;
            }
            set
            {
                _fields = value;
            }
        }
                ...
                ...
}

コントローラーのメソッド: 現在、返されるメソッドは基本的にコメント アウトされています。そこでブレークポイントを使用して、_field ディクショナリが空であることを示すオブジェクトの returnmodel を調べています。

   //
    // GET: /Diplom/Edit/5

    public ActionResult Edit(int id)
    {
        using (var db = new ProjectContext())
        {
            var d = db.Diplomas.Find(id);
            return View(d);
        }
    }

    //
    // POST: /Diplom/Edit/5

    [HttpPost]
    public ActionResult Edit(int id, Diplom returningmodel)
    {
        try
        {
            //var db = new ProjectContext();
            //db.Entry(returningmodel).State = System.Data.EntityState.Modified;
            //db.SaveChanges();

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

そして最後にView(Edit.cshtml):

@model txt.Models.Diplom

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Diplom</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.filnavn)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.filnavn)
            @Html.ValidationMessageFor(model => model.filnavn)
        </div>

        @Html.HiddenFor(model => model.Id)

        <div class="editor-label">
            @Html.LabelFor(model => model.BestaattTid)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.BestaattTid)
            @Html.ValidationMessageFor(model => model.BestaattTid)
        </div>

        <h3>Dynamic fields</h3>

        @foreach(var modelfield in Model.Fields )
        {
            <div class="editor-label">
                @Html.Label(modelfield.Key)
            </div>

            <div class="editor-field">
                @Html.EditorFor( m => modelfield.Value )
                @Html.ValidationMessage( modelfield.Value)
            </div>
        }
        <p><input type="submit" value="Save" /></p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

これを解決するにはどうすればよいですか?

4

1 に答える 1

0

デフォルトのモデル バインダーは、フォームで送信された動的フィールドのリストをモデルのディクショナリに接続できない可能性があります。これは非常に特殊な要件であるため、ポスト メソッドでモデル バインダーの代わりに FormCollection を使用して、フォームから送信されたすべてのフィールドを取得することをお勧めします。

[HttpPost]
public ActionResult Edit(int id, FormCollection form)
{
    try
    {
        foreach(var field in form.Keys)
        {
            switch (fieldToUpper().Trim())
            {
                case "FIELDNAME":
                    // assign value to your model property
                    break;
            }
        }

        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}
于 2012-09-04T08:10:03.960 に答える