[英]No values comes into FormCollection while passing data from View to controller
I am trying to save the entity in edit mode. 我正在尝试以编辑模式保存实体。 I want to pass the values from View to controller using FormCollection, but no value come into controller.
我想使用FormCollection将View的值传递给控制器,但没有值进入控制器。
Here is markup: 这是标记:
@using (Html.BeginForm("Edit", "Product", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmCreate" }))
{
<div class =" row-fluid Span12" style ="margin-bottom :15px">
<div class="span6" >
<div class ="span4">
<label>Product Name</label>
</div>
<div class ="span6">
<input type="text" value="@ViewBag.Name" id="ProductName" name="ProductName" style="height:20px; width:100%;" />
</div>
</div>
<div class="span6" >
<div class="span4" >
<label > Product Code</label>
</div>
<div class="span6" >
<input type="text" value="@ViewBag.SectionCode" id="SectionCode"/>
</div>
</div>
<div class="span11" style="text-align:right">
<input class="btn btn-primary" type="submit" value="Save" id="Edit"/>
<button class="btn btn-default">Cancel</button>
</div>
}
and this is my Controller Code 这是我的控制器代码
[HttpPost]
public ActionResult Edit(FormCollection form)
{
Product product = new Product();
product.SectionCode = Convert.ToString(form["SectionCode"]);
product.Name = Convert.ToString(form["Name"]);
return RedirectToAction("SaveData", oProductNew);
}
No value comes into FormCollection. FormCollection没有价值。
Probably, your view code does not belong to a form. 可能您的视图代码不属于表单。 Put it inside a form and try submitting it.
将其放在表单中,然后尝试提交。
How about trying this. 尝试一下。 In your
[HttpGet]
method you do the following: 在
[HttpGet]
方法中,执行以下操作:
[HttpGet]
public ActionResult Edit()
{
Product product = new Product();
return View(product);
}
For [HttpPost]
update your code to (this should work fine with binding): 对于
[HttpPost]
将您的代码更新为(与绑定结合使用应该可以正常工作):
[HttpPost]
public ActionResult Edit(Product product)
{
return RedirectToAction("SaveData", product);
}
And lastly you can update your view to take the Product as view model: 最后,您可以更新视图以将产品作为视图模型:
@model YourNamespace.Product @* Page View Model *@
@using (Html.BeginForm("Edit", "Product", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmCreate" }))
{
<div class =" row-fluid Span12" style ="margin-bottom :15px">
<div class="span6" >
<div class ="span4">
<label>Product Name</label>
</div>
<div class ="span6">
@Html.TextBoxFor(x => x.ProductName, new { @Style = "height:20px; width:100%;" }) @* TextBoxFor Extension Method *@
</div>
</div>
<div class="span6" >
<div class="span4" >
<label > Product Code</label>
</div>
<div class="span6" >
@Html.TextBoxFor(x => x.SectionCode) @* TextBoxFor Extension Method *@
</div>
</div>
<div class="span11" style="text-align:right">
<input class="btn btn-primary" type="submit" value="Save" id="Edit"/>
<button class="btn btn-default">Cancel</button>
</div>
}
The @Html
extension methods will create all the names and id's that you need for data binding to occur automagically. @Html
扩展方法将创建自动绑定数据所需的所有名称和ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.