【发布时间】:2017-08-02 10:40:00
【问题描述】:
我正在创建一个 DataEntry 屏幕,顶部有一个三个下拉列表 使用 ajax 级联。并且还使用 Ajax 呈现部分视图,该视图将显示针对该用户在下拉列表中所做的选择的所有项目。部分视图由一个包含多个卷的表组成。
我无法在我的控制器的部分视图(表)中获取用户选择的数据集合。
我的控制器
[HttpGet]
public ActionResult ApproveLaysheet()
{
LaySheetShortageViewModel mdl = new LaySheetShortageViewModel();
ViewBag.AtcID = new SelectList(db.AtcMasters.Where(o => o.IsClosed == "N"), "AtcId", "AtcNum");
return View(mdl);
}
[HttpGet]
public PartialViewResult GetRollView(decimal[] SelectedOurStyle)
{
LaySheetShortageViewModel model = new LaySheetShortageViewModel();
LaysheetRollRepository lyipores = new LaysheetRollRepository();
model.rolldetailcollection= lyipores.getlaysheetRollData(SelectedOurStyle);
return PartialView("LaySheetRollView",model);
}
[HttpPost]
public ActionResult ApproveLaysheet(LaySheetShortageViewModel Model)
{ // not gretting the value of rolldetailcollection here
return View();
}
我的观点
@model ArtWebApp.Areas.ArtMVC.Models.ViewModel.LaySheetShortageViewModel
<script type="text/javascript">
$(document).ready(function () {
$("#Show").click(function (e, params) {
debugger;
var SelectedOurStyle = new Array();
SelectedOurStyle = $("#LaySheetID").chosen().val();
if (SelectedOurStyle != null)
{
$.ajax({
url: "@Url.Action("GetRollView", "ApproveLaysheet")",
traditional: true,
data: { 'SelectedOurStyle': SelectedOurStyle },
type: "GET",
success: function (fooBarHTML) {
$("#output").html(fooBarHTML);
},
error: function (xhr, status, errorThrown) {
//...
}
});
}
});
});
</script>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>SampCutReqMaster</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="row">
<div class="col-md-2">
@Html.Label("Atcc#", new { @class = "control-label col-md-2" });
</div>
<div class="col-md-10">
@Html.DropDownList("AtcID", null, htmlAttributes: new { @class = "chosen-select form-control" })
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-2">
@Html.Label("OurStyle#", new { @class = "control-label col-md-2" });
</div>
<div class="col-md-10">
@Html.DropDownList("OurStyleID", new MultiSelectList(string.Empty, "Value", "Text") , null, htmlAttributes: new { @class = "chosen-select form-control", @multiple = "multiple" } )
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-2">
@Html.Label("LaySheet#", new { @class = "control-label col-md-2" });
</div>
<div class="col-md-10">
@Html.DropDownList("LaySheetID", new MultiSelectList(string.Empty, "Value", "Text"), null, htmlAttributes: new { @class = "chosen-select form-control", @multiple = "multiple" })
</div>
</div>
</div>
<div id='output' class="">
<!-- Partialview Comes here -->
</div>
</div>
}
我的部分观点
@using HtmlHelpers.BeginCollectionItem
@model ArtWebApp.Areas.ArtMVC.Models.ViewModel.LaySheetShortageViewModel
<script src="~/JQuery/GridJQuery.js"></script>
<script>
</script>
<div class="container">
<table class="table table-bordered table-striped table-responsive">
<tr>
<th>
@Html.CheckBox("SelectAll")
</th>
<th>
@Html.DisplayNameFor(model => model.approvelaysheetModel.LaySheetDet_PK)
</th>
<th>
@Html.DisplayNameFor(model => model.approvelaysheetModel.LayCutNum)
</th>
<th>
@Html.DisplayNameFor(model => model.approvelaysheetModel.RollNum)
</th>
</tr>
@if (Model != null)
{
for (int i = 0; i < Model.rolldetailcollection.Count; i++)
{
using (Html.BeginCollectionItem("rolldata"))
{
<tr>
<td>
@Html.EditorFor(modelItem => Model.rolldetailcollection[i].IsSelected, new { @onclick = "Check_ClickNew(this)" })
</td>
<td>
@Html.EditorFor(modelItem => Model.rolldetailcollection[i].LaySheetDet_PK)
@Html.HiddenFor(model => Model.rolldetailcollection[i].LaySheetDet_PK, new { htmlAttributes = new { @class = "form-control" } })
</td>
<td>
@Html.DisplayFor(modelItem => Model.rolldetailcollection[i].LayCutNum)
</td>
<td>
@Html.DisplayFor(modelItem => Model.rolldetailcollection[i].RollNum)
</td>
</tr>
}
}
}
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input id="Submit" type="submit" value="Submit Fabric request" class="btn btn-default show" />
</div>
</div>
</div>
我的视图模态
public class ApprovelaysheetModel
{
public Decimal ApprovelaysheetModelID { get; set; }
[Display(Name ="ID")]
public Decimal LaySheetDet_PK { get; set; }
public Boolean IsSelected { get; set; }
public Decimal LaySheet_PK { get; set; }
}
public class LaySheetShortageViewModel
{
[Key]
[Display(Name = "ID")]
public Decimal ShortageID { get; set; }
public int Location_pk { get; set; }
public int Atcid { get; set; }
public int OurstyleID { get; set; } }
public List<ApprovelaysheetModel> rolldetailcollection { get; set; }
}
任何人都可以建议我的错误是什么或任何更好的方法来做这个数据输入,因为我是 MVC 新手
【问题讨论】:
-
您的代码存在多个问题,但您的集合未绑定的原因是您使用
BeginCollectionItem()为您的name属性添加前缀rolldata[xxx]与您的模型无关。您为什么使用BeginCollectionItem()(仅用于从您的收藏中动态添加和删除您似乎没有做的项目)?如果您确实想要该功能,则不要使用for循环 -
嗨,步骤我之前尝试过没有开始收集,只是使用了 foreach(Model.rolldetailcollection 中的 var 项),但它没有帮助我现在使用开始收集将 rolldetail 收集设为空,它正确显示了计数但是里面的物品没有绑定。我需要在回发中获取表格的所有行。
-
您不能使用
foreach循环(尽管如果您使用BeginCollectionItem则需要这样做,但这是一个不同的问题)。您需要一个for循环或EditorTemplate(请参阅this answer)。如果您没有在视图中动态添加和删除集合中的项目,则只需删除using (Html.BeginCollectionItem("rolldata"))代码行,它将正确绑定 -
但是您的代码存在很多或不相关的问题。建议您研究this DotNetFiddle 中的代码,了解如何生成级联下拉列表。还有this answer,为什么在创建
<select multiple>时必须使用ListBoxFor()而不是DropDownListFor()。 -
当然可以,但需要 30 分钟左右。我将添加更多注释(您的视图模型应更改为允许强类型绑定)。我知道你在使用
chosen,但这没什么区别
标签: c# ajax asp.net-mvc asp.net-mvc-4