[英]Pass a list from a view to a controller, MVC 5
So, I'm using MVC, and I'm trying to create a new employee using entity. 因此,我正在使用MVC,并且正在尝试使用实体创建新员工。 Along with creating the employee, I am trying to create a list of items that are tied to the employee (one to many).
在创建员工的同时,我试图创建与员工相关的项目列表(一对多)。
So my issue is, I am calling a javascript function in the onclick event of the submit button. 所以我的问题是,我在提交按钮的onclick事件中调用了javascript函数。 This javascript function creates a list of IDs, I need these to create the items.
此javascript函数创建ID列表,我需要这些ID来创建项目。
How can I get this list of IDs to my controller, so I can create them? 如何将ID列表发送到控制器,以便创建它们?
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" onclick="getResult()"/>
</div>
</div>
Add an empty hidden input to the form element in your view, let's call it idslist 将空的隐藏输入添加到视图中的form元素,我们将其称为idslist
@Html.Hidden("idslist")
put your ids in that input using javascript JSON.stringify or simply an id list separated by a separator. 使用javascript JSON.stringify或仅由分隔符分隔的ID列表,将您的ID放入该输入中。
And now you can read that list on your controller, when the user submits the form : 现在,当用户提交表单时,您可以在控制器上阅读该列表:
public ActionResult Create(myModel x, string idslist)
{
// now you have to deserialize your idslist
// using JsonConvert.DeserializeObject or just split for the second case.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.