检索MVC GET方法里面模特属性值 [英] retrieve model properties values inside GET method in mvc
问题描述
我有以下 GET
的方法,这是一个code到创建表单
I have following GET
method , that's a code to a create form
public ActionResult Add_Product(string Product_ID)
{
AddNewProduct sample = new AddNewProduct();
return View(sample);
}
这是该模型类
public class AddNewProduct
{
public string Product_ID { get; set; }
...
}
这是创建表单
@model project_name.Models.AddNewProduct
<h4>Add New Product</h4>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group">
@Html.LabelFor(model => model.Product_ID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Product_ID, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Product_ID, "", new { @class = "text-danger" })
</div>
</div>
.....
<div>
@Html.ActionLink("Back to AddNewProduct", "AddNewProduct","Home" , new {Product_ID = Model.Product_ID})
</div>
}
我可以用这个视图页面。但是当我点击这个返回AddNewProduct
链接和调试 AddNewProduct
我看不到任何价值字符串的product_id
I can Insert a Product_ID using this view page .But Once I click this Back to AddNewProduct
link and debug AddNewProduct
I cannot see any value for string Product_ID
为什么这个模特属性没有很好地结合
Why this model properties not bind well
推荐答案
要通过文本框的值 Add_Product()
GET方法,则需要使用JavaScript / jQuery的。更换你 @ Html.ActionLink(..)
与
To pass the value of the textbox to the Add_Product()
GET method, you need to use javascript/jquery. Replace you @Html.ActionLink(..)
with
<a href="#" class="back">Back to AddNewProduct</a>
和添加下面的脚本
var baseUrl = '@Url.Action("Add_Product", "Home")';
$('#back').click(function() {
var id = $('#Product_ID').val();
location.href = baseUrl + '/' + id;
}}
请注意: location.href = +的baseUrl'/'+ ID;
假定您的已定义的特定路由与 {控制器} / {行动} / {PRODUCT_ID}
,否则它需要
Note: location.href = baseUrl + '/' + id;
assumes your have defined a specific route with {controller}/{action}/{Product_ID}
, otherwise it needs to be
location.href = baseUrl + '?Product_ID=' + id;
另外,改变方法参数,以字符串ID
所以它使用默认路由
还要注意的是,你可能会想方法更改为
Note also that you will probably want to change the method to
public ActionResult Add_Product(string Product_ID)
{
AddNewProduct sample = new AddNewProduct
{
Product_ID = Product_ID
};
return View(sample);
}
因此,如果您单击返回AddNewProduct
链接,视图将显示输入previous值。
so that if you click the Back to AddNewProduct
link, the view will display the previous value you entered.
这篇关于检索MVC GET方法里面模特属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!