2013-01-23 31 views
2

我不知道是什麼原因,該模型是沒有約束力的創建操作:型號不綁定到空行動作爲傳遞價值

視圖模型:

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> 
} 

Action Post:

[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行動的接觸參數「空」,爲什麼會出現這種情況?

謝謝

+1

怪異的,所有看起來沒問題,當你調試你的動作方法什麼在'Request.Form'集合? – mattytommo

+0

你可以發佈你的HttpGet動作嗎? – mattytommo

+0

您是否嘗試過使用@Html.EditorFor而不是TextBoxFor。我嘗試只使用TextBoxFor當我需要指定長度 – Robert

回答

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> 
} 
+0

嗨,謝謝,但它仍然不起作用 – Patrick

+0

當模型傳遞迴控制器時,您的Address_Id屬性爲空嗎? – Robert

+0

嗨,接觸參數是在後回零和request.from是的Request.Form {__RequestVerificationToken = n8AM-8LxejrP394kQSs9hKHvrVCjRO2mhK1ZKg3oQNj-zfS3rLLyuQ83HIqqpI-OWZqdiedjQcSVzfes-PQTK7LB6vqmyW97wI6uAubbN-XeDgbrPeMF4CBhq7STNRu00&ADDRESS_ID = 1041&ContactType_Id = 3&聯繫= 965420760}你可以看到ADDRESS_ID具有價值1041 – Patrick

3

難以置信,我發現這個問題,動作的參數在視圖模型中字段的名稱相同,的變化參數名稱和一切作品發現:

[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)); 
    }