0

コントローラーメソッドの取得:

    public ActionResult Create(int? parentId)
    {
        var model = new CreatePersonViewModel();

        // pull set from db
        var parent = _db.Persons.FirstOrDefault(s => s.PersonId == parentId);

        if (parent != null)
        {
            model.ParentId = parent.PersonId;
        }

        return View("Create", model);
    }

役職:

public ActionResult Create(CreatePersonViewModel viewModel) // viewModel.ParentId is 0 when it's passed to this method from the view, even though I didn't set it, and it's null coming from GET.  
        {
            //bla bla
        }

私の見解:

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>CreatePersonViewModel</legend>

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

        @Html.HiddenFor(model => model.ParentId) // tried with second parameter of "null" as well

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

ビューがnullである必要がある場合に、ビューがそのプロパティの値0を送り返す理由はありますか?ここに追加する詳細はありませんが、フォームの検証でさらにテキストが要求されるため、ここに追加します。

4

0 に答える 0