我有以下列表视图
点击Reject
后,我刚配置为打开模式对话框,如下面的
所以一旦我提交了这个模态对话框,我想向方法提交模态值,以便将其保存在数据库表中。
这是相关的模型类
public class ProductViewModel
{
public AB_Product Products { get; set; }
public IEnumerable<AB_Product> LProducts { get; set; }
}
public partial class AB_Product
{
public string ProductID { get; set; }
public string ApprovalStatus { get; set; }
public string ReasonEn { get; set; }
public string ReasonAr { get; set; }
}
这是查看页面代码
@model project_name.Models.ProductViewModel
<table class="table">
<tr>
<th>
Name
</th>
<th>
Action
</th>
</tr>
@foreach (var obj in Model.LProducts)
{
<tr>
@Html.HiddenFor(modelItem => obj.ProductID)
<td>
@Html.DisplayFor(modelItem => obj.ProductTitleEn)
</td>
<td>
<div class="btn-group btn-group-sm" id="CreateButton">
<button type="button" class="btn btn-default" onclick="location.href='@Url.Action("Create")';return false;">View Product</button>
</div>
@if (obj.ApprovalStatus == "Pending")
{
<div class="btn-group btn-group-sm" id="ApproveButton">
<button type="button" class="btn btn-success" onclick="location.href='@Url.Action("Change_Product_State","Home", new { productid = obj.ProductID, value = "Approved" },null)';return false;">Approve</button>
</div>
<div class="btn-group btn-group-sm" id="RejectButton">
<button type="button" id="modal-opener" class="btn btn-danger" return false;">Reject</button>
</div>
}
<div class="btn-group btn-group-sm" id="CreateButton">
<button type="button" class="btn btn-primary" onclick="location.href='@Url.Action("Create_ProductComments","Home", new { productid = obj.ProductID }, null)';return false;">Add Comments</button>
</div>
</td>
</tr>
}
</table>
<div id="dialog-modal" title="Basic modal dialog">
@using (Ajax.BeginForm("Change_Product_State", "Home", new { value = "Rejected" }, new AjaxOptions { UpdateTargetId = "ID", OnSuccess = "onSuccess" }))
{
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.Products.ReasonEn)
</div>
<div class="editor-field">
@Html.TextAreaFor(m => m.Products.ReasonEn)
@Html.ValidationMessageFor(m => m.Products.ReasonEn)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Products.ReasonAr)
</div>
<div class="editor-field">
@Html.TextAreaFor(m => m.Products.ReasonAr)
@Html.ValidationMessageFor(m => m.Products.ReasonAr)
</div>
<p>
<input type="submit" value="Submit" />
</p>
</fieldset>
</div>
}
</div>
@section Scripts
{
<script>
$(function () {
$("#dialog-modal").dialog({
autoOpen: false,
width: 400,
height: 400,
show: {
effect: "blind",
duration: 1000
},
hide: {
effect: "explode",
duration: 1000
}
});
$("#modal-opener").click(function () {
$("#dialog-modal").dialog("open");
});
});
function onSuccess() {
$("#dialog-modal").dialog("close");
}
</script>
}
如何绑定列表对象我单击ProductID
与m.Products.ProductID
答案 0 :(得分:2)
从@Html.HiddenFor(modelItem => obj.ProductID)
循环和foreach
添加
Ajax.BeginForm()
@Html.HiddenFor(m => m.Products.ProductID)
所以它的价值可以发布。然后将ProductID
的值添加为按钮的data-
属性(并将id
更改为class
- 请参阅下面的注释)
<button type="button" class="modal-opener btn btn-danger" data-id="@obj.ProductID">Reject</button>
并将脚本修改为
$(".modal-opener").click(function () { // use class name
$('#Products_ProductID').val($(this).attr("data-id")); // set the value of the hidden input
$("#dialog-modal").dialog("open");
})
请注意,由于id
循环生成了重复的foreach
属性,您有很多无效的html。请改用类名。你的HTML应该是
@foreach (var obj in Model.LProducts)
{
<tr>
<td>@Html.DisplayFor(modelItem => obj.ProductTitleEn)</td>
<td>
<div class="btn-group btn-group-sm CreateButton"> // change
<button type="button" class="btn btn-default" onclick="location.href='@Url.Action("Create")';return false;">View Product</button>
</div>
@if (obj.ApprovalStatus == "Pending")
{
<div class="btn-group btn-group-sm ApproveButton"> // change
<button type="button" class="btn btn-success" onclick="location.href='@Url.Action("Change_Product_State","Home", new { productid = obj.ProductID, value = "Approved" },null)';return false;">Approve</button>
</div>
<div class="btn-group btn-group-sm RejectButton"> // change
<button type="button" class="modal-opener btn btn-danger" data-id="@obj.ProductID">Reject</button> // as above
</div>
}
<div class="btn-group btn-group-sm CreateButton"> // change
<button type="button" class="btn btn-primary" onclick="location.href='@Url.Action("Create_ProductComments","Home", new { productid = obj.ProductID }, null)';return false;">Add Comments</button>
</div>
</td>
</tr>
}