<div id="4591" >
<input type="text" id="Title1" name="Title1" value="a" />
<input type="submit" name="button" value="Save" /> </div>
<div id="4592" >
<input type="text" id="Title2" name="Title2" value="a" />
<input type="submit" nam开发者_如何学Goe="button" value="Save" /> </div>
<div id="4593" >
<input type="text" id="Title3" name="Title3" value="a" />
<input type="submit" name="button" value="Save" /> </div>
This is the copy paste version of the html source generated by the browser which is making it clear that i am generating the dynamic fields on the page. name in the textbox is the field in the database. After pressing the one of the save buttons how would i send the particular textbox name and value to the controller action to be updated.
Give your submit buttons a different name:
<div id="4591">
<input type="text" id="Title1" name="Title1" value="a" />
<input type="submit" name="button4591" value="Save" />
</div>
<div id="4592">
<input type="text" id="Title2" name="Title2" value="a" />
<input type="submit" name="button4592" value="Save" />
</div>
<div id="4593">
<input type="text" id="Title3" name="Title3" value="a" />
<input type="submit" name="button4593" value="Save" />
</div>
And then in your controller action check the request parameters. You will see that a parameter with the name of the clicked button will be passed:
[HttpPost]
public ActionResult Index()
{
string id = Request.Params
.Cast<string>()
.Where(p => p.StartsWith("button"))
.Select(p => p.Substring("button".Length))
.First();
return View();
}
If someone just stuck on that problem but using newer version of .net I strongly recommend to give buttons different names and use Request.Form
HTML code:
<form asp-controller="yourcontroller" asp-action="youraction" method="post">
<button name="one" class="btn btn-light">one</button>
<button name="two" class="btn btn-light">two</button>
<button name="three" class="btn btn-light">three</button>
</form>
and method action:
[HttpPost]
public IActionResult YourAction()
{
var buttonNames = Request.Form.Select(x => x.Key).ToList();
//now you can see clicked button name from form
string buttonName = buttonNames.FirstOrDefault();
//do your logic...
return View();
}
精彩评论