[英]ASP MVC Controller post same html input name with different values
I have this input type that needs to be cloned sample html is below. 我有此输入类型,需要复制,示例html在下面。
<input type="text"id="person" name="Person" class="person-list" />
<input type="text"id="person" name="Person" class="person-list" />
I want to get the values of the two input through MVC. 我想通过MVC获取两个输入的值。 The code below is my current work but it only gets the value of the first input.
下面的代码是我当前的工作,但它仅获取第一个输入的值。
[HttpPost]
public ActionResult ConfirmationPage(string Person){
}
Your method needs to be 您的方法需要
public ActionResult ConfirmationPage(string[] Person){ // or IEnumerable<string>
However your also creating invalid html because of the duplicate id
attributes so you should remove those from the inputs as well. 但是,由于
id
属性重复,您还会创建无效的html,因此您也应该从输入中删除这些属性。
Try This. 尝试这个。
[HttpPost]
public ActionResult ConfirmationPage(string[] Person){
}
<input type="text" name="Person" class="person-list" />
<input type="text" name="Person" class="person-list" />
I think your problem is fixed. 我认为您的问题已解决。 Stephen Muecke's answer is absolutely correct.
Stephen Muecke的答案是绝对正确的。 I just want to add one point to that.
我只想补充一点。
You can specify indices of your array elements from your html or view page. 您可以从html或view页面中指定数组元素的索引。
<!-- You are specifying that this input should be Person[0] -->
<input type="text" name="Person[0]" class="person-list" />
<input type="text" name="Person[1]" class="person-list" />
I know this is just a small point, but some times you can use this. 我知道这只是一个小问题,但是有些时候您可以使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.