我在MVC中发送checkboxes
和EditorFor
值时遇到麻烦。我有两节课:
public class CompleteReceiving
{
public List<SiteOrderReceiving> order_detail { get; set; } //<- Data from DB. It contains all items for Order Receiving.
public List<SomeClass> new_form { get; set; } //<- Fields for form
}
public class SomeClass
{
[RegularExpression("^[0-9]*$", ErrorMessage = "Receive Quantity can only contain number")]
public decimal? receive_quantity { get; set; }
[RegularExpression("^[0-9]*$", ErrorMessage = "Damaged Quantity can only contain number")]
public decimal? damaged_quantity { get; set; }
public bool IsSelected { get; set; }
}
这是我的观点:
@for(int i = 0; i < Model.order_detail.Count; i++)
{
<tr>
<td>@Html.CheckBoxFor(m => m.new_form[i].IsSelected, new { id = "site_rec_checkbox", @class= "site_rec_checkbox" })
</td>
<td>@Html.EditorFor(m => m.new_form[i].receive_quantity, new { htmlAttributes = new { @class = "form-control fm", @autocomplete = "off", @autofocus = "autofocus", @placeholder = "Receiving Quantity" } })
@Html.ValidationMessageFor(m => m.new_form[i].receive_quantity, "", new { @class = "text-danger" })
</td>
}
这是我的控制器操作代码:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SiteOrderReceiving(CompleteReceiving sor)
{
//Code for post
}
问题在于,只要我在checkbox
或index
以外的其他1
中选择2
,List
始终为null
。但是,如果我第一次选择3
或4
,然后选择1
或2
,那还是不错。
我不知道我在做什么错。
任何帮助将非常感激。
更新
这是我的控制器Action Code
[HttpGet]
public ActionResult SiteOrderReceiving(int? order_id)
{
var get_detail = (from oa in db.order_send
where oa.order_id == order_id
select new SiteOrderReceiving()
{
quantity_send = oa.send_quantity,
date_send = oa.send_date,
order_id = oa.order_id
}).ToList();
var a = new CompleteReceiving();
a.order_detail = get_detail;
return View(a);
}
这是我的View
@using (Html.BeginForm("SiteOrderReceiving", "Site", FormMethod.Post, new { id = "receive_form" }))
{
@Html.AntiForgeryToken();
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@for(int i = 0; i < Model.order_detail.Count; i++)
{
<tr>
<td>@Html.CheckBoxFor(m => m.new_form[i].IsSelected, new { id = "site_rec_checkbox", @class= "site_rec_checkbox" })
</td>
<td>@Html.EditorFor(m => m.new_form[i].receive_quantity, new { htmlAttributes = new { @class = "form-control fm", @autocomplete = "off", @autofocus = "autofocus", @placeholder = "Receiving Quantity" } })
@Html.ValidationMessageFor(m => m.new_form[i].receive_quantity, "", new { @class = "text-danger" })
</td>
}
}
答案 0 :(得分:2)
所以我解决了我的问题。我所做的是将form
的属性移到现有类中,在该类中我正在从DB
获取数据。
现在我的ViewModel
看起来像这样:
public class CompleteReceiving
{
public List<SomeClass> order_detail { get; set; }
//Some Other properties
}
然后:
public class SomeClass
{
[RegularExpression("^[0-9]*$", ErrorMessage = "Receive Quantity can only contain number")]
public decimal? receive_quantity { get; set; }
[RegularExpression("^[0-9]*$", ErrorMessage = "Damaged Quantity can only contain number")]
public decimal? damaged_quantity { get; set; }
public bool IsSelected { get; set; }
public string item_name { get; set; }
}
像上面那样更新我的code
之后,一切开始正常运行。希望它对以后的人有所帮助。