简体   繁体   English

ASP MVC Controller发布具有不同值的相同html输入名称

[英]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.

相关问题 html抓取,获得具有相同名称的不同值 - html scraping, getting different values with the same name ASP .NET Core MVC-将HTML选择控件中的列表数据发布到MVC控制器中 - ASP .NET Core MVC - Post List data from HTML select control into MVC Controller 从具有相同类名的不同输入字段获取值 - Getting values from different input fields with same class name 根据检查状态为 HTML 复选框输入中的名称分配不同的值 - Assigning different values to a name in HTML checkbox input based on check status Spring MVC - 从 controller POST 到不同的 url - Spring MVC - POST to different url from controller 将数据从 asp.net mvc 中的 2 个不同 jquery 传递到同一控制器 - Pass data to same Controller from 2 different jquerys in asp.net mvc 将隐藏的输入元素值发布到控制器? - Post Hidden Input Element Values to Controller? 具有相同输入值的不同哈希 - Different hashes with same input values 在ASP.NET MVC中通过AJAX从操作中获取html数据后更改输入名称属性 - Change input name attributes after getting html data from action through AJAX in ASP.NET MVC 如何通过Ajax POST传递多个具有相同名称的输入数组元素值,例如ex:sikll [] - How to pass Multiple input array element values having same name like ex: sikll[] via Ajax POST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM