设定值从集合提交提交MVC [英] set value submit from collection submit mvc
问题描述
我想从采集控制器和模型和具体的价值得到这等于行,我preSS按钮
I want to get in controller and Model and specific value from collection which equal line on which I press button
<table id="Products" class="Products">
<tr>
<th>ProductId</th>
<th>Productname</th>
<th>Quantity</th>
<th>UnitPrice</th>
</tr>
<% for(int i=0; i < Model.NorthOrderDetails.Count; i++)
{ %>
<tr>
<td><%: Html.Label(Model.NorthOrderDetails[i].ProductID.ToString()) %></td>
<td><%: Html.Label(Model.NorthOrderDetails[i].ProductName) %> </td>
<td><%: Html.TextBoxFor(m => m.NorthOrderDetails[i].Quantity) %></td>
<td><%: Html.TextBoxFor(m => m.NorthOrderDetails[i].UnitPrice) %></td>
<td><%: @Html.ActionLink("Go to second view", "ViewTwo", "Order", Model, null)%></td>
<input type="submit" title="ads" value =<%: Model.NorthOrderDetails[i].ProductID.ToString()%> name=ssad />
<tr>
<% } %>
</table>
我可以设置值从收集提交,例如:
Can I set value in submit from collection, for example
<input type="submit" title="ads" value =<%: Model.NorthOrderDetails[i].ProductID.ToString()%> name=ssad />
和该值将在控制器等于17,例如。这项工作,但我怎么能文本的按钮从价值集合任何文本改变?
And this value will equal 17, for example in controller. This work, but how I can change of text in button from value in collection to any text?
更新
我用斯蒂芬Muecke的code,但因为我使用aspx页面我编辑表
UPDATE I use code of Stephen Muecke, but I edit table because I use aspx page
<td><button type="button" class="delete" data-id="<%:Model.NorthOrderDetails[i].ProductID %>">Delete</button><td>
<td><input type="hidden" name="<%:Model.NorthOrderDetails[i].ProductName %>" value="<%:i %>" /><td>
和,不幸的是,脚本不呼叫控制器
And, unfortunately the script doesn't call controller
推荐答案
而不是做一个完整的职位和再生的观点,每次要删除的项目,您可以使用Ajax的ID值张贴的项目到控制器的方法即删除数据库中的项目,然后从DOM中移除该项目。这将极大地提高性能和手段,你也许能避免使用会话
。
Rather than doing a full post and regenerating the view each time you want to delete an item, you can use ajax to post the items ID value to a controller method that deletes the item in the database and then remove that item from the DOM. This will greatly improve performance and means you can probably avoid using Session
.
更改视图(抱歉,这里是Razor语法)
Change the view to (sorry, but this is Razor syntax)
@for (int i = 0; i < Model.NorthOrderDetails.Count; i++)
{
<tr>
<td>@Html.LabelFor(Model.NorthOrderDetails[i].ProductID)</td> // ToString not required
<td>@Html.Label(Model.NorthOrderDetails[i].ProductName)</td>
<td>@Html.TextBoxFor(m => m.NorthOrderDetails[i].Quantity)></td>
<td>@Html.TextBoxFor(m => m.NorthOrderDetails[i].UnitPrice)</td>
<td>@Html.ActionLink("Go to second view", "ViewTwo", "Order", Model, null)</td> // This wont work
<td>
<button type="button" class="delete" data-id="@Model.NorthOrderDetails[i].ProductID">Delete</button><td> // change this
<input type="hidden" name="@Model.NorthOrderDetails.Index" value="@i" /> // add this
</tr>
}
</table>
<input type="submit" value="Save" /> // add this
注:
- 您的操作链接将无法正常工作(你不能传递一个集合到一个
GET方法),我怀疑你的意思是@ Html.ActionLink(转到第二个观点,ViewTwo,订单,新{ID = Model.NorthOrderDetails [I] .ProductID},NULL)
这样你就可以通过产品ID为ViewTwo()
法 - 更改每一行提交按钮正常按钮,添加一个在年底提交按钮(所有更改的文本框保存在一个职位)
- 添加特殊的隐藏输入一个
首页
属性。这是用来
由DefaultModelBinder
来匹配的集合,其中
索引是不连续的(其中,如果你删除的项目,他们将
在收集的中间) - 您不会使任何输入
的ProductID
,这意味着你不会
能够识别后的产品回来。您将需要添加
它隐藏的输入
- Your action link will not work (your cannot pass a collection to a
GET method) I suspect you mean
@Html.ActionLink("Go to second view", "ViewTwo", "Order", new { ID = Model.NorthOrderDetails[i].ProductID }, null)
so you can pass the productID to theViewTwo()
method - Change the submit button in each row to a normal button, and add one submit button at the end (to save all changes to your textboxes in one post)
- Add the special hidden input for an
Index
property. This is used by theDefaultModelBinder
to match up collections where the indexers are non-consecutive (which they will be if you delete items in the middle of the collection) - You don't render any input for the
ProductID
which means you wont be able to identify the products on post back. You will need to add a hidden input for it
然后添加下面的脚本
var url = '@Url.Action("Delete", "YourControllerName")';
$('.delete').click(function() {
var id = $(this).data('id'); // Get the product ID
var row = $(this).closest('tr') // Get the table row
$.post(url, { ID: id }, function(data) {
if(data) {
row.delete(); // remove the row from the table
} else {
// oops!
}
});
});
和控制器
public ActionResult View(IEnumerable<YourModel> model)
{
// Save your collection and redirect
}
[HttpPost]
public JsonResult Delete(int ID)
{
// Delete the product in the database based on the ID
return Json(true);
}
请注意:如果删除的项目可能会引发并以某种方式失败的例外,那么你应该返回JSON(NULL);
,因此它可以在AJAX方法进行检查
Note: If deleting an item could throw and exception of fail in some way, then you should return Json(null);
so it can be checked in the ajax method.
这篇关于设定值从集合提交提交MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!