tags:

views:

39

answers:

1
 <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" name="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.

+1  A: 

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();
}
Darin Dimitrov
well certainly i would say a wonderful solution
mazhar kaunain baig