[英]Post data to controller action method in MVC 5 after adding items dynamically
I have a model which is as below 我有一个如下模型
public class ServiceView
{
public ServiceView()
{
SubServiceView = new List<SubServiceView>();
}
public int Id { get; set; }
public string Name { get; set; }
public List<SubServiceView> SubServiceView { get; set; }
}
public class SubServiceView
{
public int Id { get; set; }
public int ParentId { get; set; }
[Required(ErrorMessage="Name is Required")]
public string Name { get; set; }
public string Description { get; set; }
}
I am able to load items dynamically but after posting I got SubServiceView count 0 always. 我能够动态加载项目,但发布后我的SubServiceView总计数为0。 Below is my code. 下面是我的代码。
Create View: 创建视图:
<table class="form-group">
<thead>
<tr>
<td class="control-label col-md-2">
<label>Name</label>
</td>
<td class="col-md-10">
<input id="SubServiceView_0_Name" name="WeDoShoesCMSAdminPanel.ViewModel.SubServiceView[0].Name" type="text" class="form-control" value="" />
</td>
</tr>
<tr>
<td class="control-label col-md-2">
<label>Description</label>
</td>
<td class="col-md-10">
<input id="SubServiceView_0_Description" name="WeDoShoesCMSAdminPanel.ViewModel.SubServiceView[0].Description" type="text" class="form-control" value="" />
</td>
</tr>
</thead>
<tbody id="tbSubServices"></tbody>
</table>
Script function: 脚本功能:
function addSubService() {
@{
Model.SubServiceView.Add(new WeDoShoesCMSAdminPanel.ViewModel.SubServiceView());
}
var index = $("#tbSubServices").children("tr").length;
//var indexCell = "<td style='display:none'><input name='SubServiceView.Index' type='hidden' value='" + index + "' /></td>";
var labelNameCell = "<td class='control-label col-md-2'><label>Name</label></td>";
var nameCell = "<td class='col-md-10'><input id='SubServiceView_" + index + "_Name' name='WeDoShoesCMSAdminPanel.ViewModel.SubServiceView[" + index + "].Name' type='text' class='form-control' value='' /></td>";
var labelDescriptionCell = "<td class='control-label col-md-2'><label>Description</label></td>";
var descriptionCell = "<td class='col-md-10'><input id='SubServiceView_" + index + "_Description' name='WeDoShoesCMSAdminPanel.ViewModel.SubServiceView[" + index + "].Description' type='text' class='form-control' value='' /></td>";
var removeCell = "<td class='col-md-10'><input id='RemoveSubService' type='button' class='btn btn-default' value='-' onclick='removeSubService(" + index + ");' /></td>";
var newRow = "<tr id='trSubServices" + index + "'>" +
labelNameCell + nameCell + removeCell + "</tr>" +
"<tr id='trSubServices1" + index + "'>" +
labelDescriptionCell + descriptionCell + "</tr>";
$("#tbSubServices").append(newRow);
}
function removeSubService(id) {
var controlToBeRemoved = "#trSubServices" + id;
var controlToBeRemoved1 = "#trSubServices1" + id;
$(controlToBeRemoved).remove();
$(controlToBeRemoved1).remove();
}
Also validation is not working on dynamically added sub-services so I don't know whether this is good solution or not. 另外,验证不适用于动态添加的子服务,因此我不知道这是否是一个好的解决方案。 As I am new in MVC so can anyone guide me how to achieve the same requirement in some optimized and generic way? 由于我是MVC的新手,所以有人可以指导我如何以某种优化和通用的方式达到相同的要求吗?
You can try like this using linq 您可以使用linq这样尝试
To Add 加上
ProductView.SizeTypes.Add(new SizeTypes(){
//Your items
});
To Remove 去除
ProductView.SizeTypes.Remove(SizeTypes);
You can try same for Brands
您可以为Brands
尝试相同的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.