2

理由はわかりませんが、モデルが Create アクションにバインドされていません。

ビューモデル:

public class AddressContactViewModel
{
    // Primary properties
    public int Id { get; set; }
    public string Contact { get; set; }

    // Navigation properties

    [Display(ResourceType = typeof(HeelpResources), Name = "AddressContactViewModelNameLabel")]
    [Required(ErrorMessageResourceName = "ErrorMsgRequiredField", ErrorMessageResourceType = typeof(HeelpResources))]
    public int ContactType_Id { get; set; }

    public int Address_Id { get; set; }

    // ViewModel dropdownlists
    public IEnumerable<SelectListItem> ContactTypeList { get; set; }
}

意見:

@model Heelp.ViewModels.AddressContactViewModel

@using (Html.BeginForm()) {

    @Html.AntiForgeryToken()

    @Html.HiddenFor(m => m.Address_Id)

    <fieldset>
        <legend>AddressContactViewModel</legend>

       <div id="year">
        @Html.DisplayNameFor(m => m.ContactType_Id)
        @Html.DropDownListFor(m => m.ContactType_Id, Model.ContactTypeList, HeelpResources.AddressContactViewModelContactTypesListDropDownFirstRecord)
        @Html.ValidationMessageFor(m => m.ContactType_Id)
    </div>
    <div class="editor-label">
        @Html.LabelFor(m => m.Contact)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(m => m.Contact)
        @Html.ValidationMessageFor(m => m.Contact)
    </div>

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

アクション投稿:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public virtual ActionResult ContactCreate(AddressContactViewModel contact)
    {
        var contactDto = Mapper.Map<AddressContactViewModel, AddressContactDto>(contact);

        _addressContactService.Create(contactDto);

        return RedirectToAction(MVC.Address.ContactList());
    }

編集:取得:

    public virtual ActionResult ContactCreate(int addressId)
    {
        var model = new AddressContactViewModel
            {
                Address_Id = addressId,
                ContactTypeList = ContactTypeDropDownList()
            };

        return View(model);
    }

フォームを送信すると、ContactCreate アクションの contact パラメータに「null」が表示されます。これはなぜですか?

ありがとう

4

2 に答える 2

3

信じられないことに、問題が見つかりました。アクションのパラメーターは、ViewModel のフィールドと同じ名前であり、パラメーター名を変更すると、すべてが機能します。

    [HttpPost]
    [ValidateAntiForgeryToken]
    public virtual ActionResult ContactCreate(AddressContactViewModel addressContact) // HERE I CHANGE FROM contact TO addressContact AND THE PROBLEM GET SOLVED
    {
        var contactDto = Mapper.Map<AddressContactViewModel, AddressContactDto>(addressContact);

        _addressContactService.Create(contactDto);

        return RedirectToAction(MVC.Address.ContactList(addressContact.Address_Id));
    }
于 2013-01-23T16:11:04.520 に答える
0

前回のアプリで同じ問題に遭遇しました。送信するボタンとフィールドを同じ div に含めてみてください。

@model Heelp.ViewModels.AddressContactViewModel

@using (Html.BeginForm()) {

@Html.AntiForgeryToken()

@Html.HiddenFor(m => m.Address_Id)

<fieldset>
    <legend>AddressContactViewModel</legend>

   <div id="year">
    @Html.DisplayNameFor(m => m.ContactType_Id)
    @Html.DropDownListFor(m => m.ContactType_Id, Model.ContactTypeList, HeelpResources.AddressContactViewModelContactTypesListDropDownFirstRecord)
    @Html.ValidationMessageFor(m => m.ContactType_Id)
</div>
<div class="editor-label">
    @Html.LabelFor(m => m.Contact)
</div>
<div class="editor-field">
    @Html.TextBoxFor(m => m.Contact)
    @Html.ValidationMessageFor(m => m.Contact)
    <input type="submit" value="Create" />
</div>
</fieldset>
}
于 2013-01-23T12:57:24.913 に答える