2016-06-21 61 views
-2

我想從我的表單提交ProductBrandViewModel類型的視圖模型我的控制器動作,但在視圖模型兩端的物業name即使已從表單中傳入值,它仍爲null。上一個動作表單POST日常工作而不是其他時候正好相同方式實現

<div class="modal fade" id="modAddProductBrand" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
    <div class="modal-dialog" role="document"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
       <h4 class="modal-title">Add Brand</h4> 
      </div> 
      <div class="modal-body"> 
       @using (Html.BeginForm("Create", "ProductBrands", FormMethod.Post, new {id = "frmProductBrand"})) 
       { 
        @Html.AntiForgeryToken() 
        @Html.ValidationSummary(false, "You have not entered a name for your brand", new {@class="alert alert-danger"}) 
        <div class="form-group"> 
         @Html.LabelFor(model => model.ProductBrand.Name) 
         @Html.TextBoxFor(model => model.ProductBrand.Name, null, new {id = "txtProductBrandName", @class = "form-control", placeholder = "Brand Name"}) 
        </div> 
        <div class="modal-footer"> 
         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
         <button type="submit" class="btn btn-success" id="btnCreateProductBrand"> Save Changes</button> 
        </div> 
       } 
      </div> 
     </div> 
    </div> 
</div> 

POST操作

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(ProductBrandViewModel brand) 
    { 
     if (!ModelState.IsValid) return null; 
     try 
     { 
      var userSessionViewModel = GetUserSession(); 
      var createdProductBrand = CreateProductBrand(userSessionViewModel.Business, brand); 
      return RedirectToAction("Index"); 
     } 
     catch 
     { 
      return View("Index"); 
     } 
    } 

ProductBrandViewModel

public class ProductBrandViewModel 
{ 
    public Guid Id { get; set; } 

    [Required] 
    [Display(Name = "Brand Name")] 
    public string Name { get; set; } 

    public int ProductCount { get; set; } 
} 

回答

0

所以我想通了,因爲窗體產生的輸入字段爲ProductBrand.Name,th使用brand作爲參數將不起作用,因爲它沒有ProductBrand.Name的屬性,通過將參數名稱從brand更改爲productBrand,MVC能夠匹配並且能夠正確地爲Name屬性賦值。

這正是我的類別操作起作用的原因,因爲參數名稱是productCategory而不僅僅是category

0

在你科瑞E查看,您使用的是像

Html.TextBoxFor(model => model.ProductBrand.Name) 

這個TextBoxFor helper方法會生成名稱的輸入域「ProductBrand.Name

<input name="ProductBrand.Name" type="text" value="" /> . 

但在HttpPost動作,參數爲ProductBrandViewModel類型的做的沒有任何姓名ProductBrand.Name

的解決方案是從

public ActionResult Create(ProductBrandViewModel brand) 
{} 

更改HttpPost行動的參數類型

public ActionResult Create(ProductBrandPageViewModel brand) 
{} 

或更新您的觀點有輸入字段生成的字段名稱爲Name代替ProductBrand.Name

+0

嗨,謝謝你的迴應,但是如果你注意到了,我已經提到了改變這個論點,正如你所說的那樣。但是在我的問題中有一個部分叫做工作版本,我對類別的執行方式完全相同。控制器接受ProductCategoryViewModel而不是ProductCategoryPageViewModel,它工作正常。爲什麼當我嘗試爲品牌實現相同的事情時,它不起作用 –

+0

對於工作版本,你是在做一個ajax發佈或一個正常形式的帖子,因爲它是?在兩個屏幕中檢查生成的輸入表單名稱。 – Shyju

+0

它們都是正常的表單發佈,此時不使用AJAX。 –

相關問題