0

In my MVC project, I created a table from a database I made, and added Edit/Save and Delete buttons to interact with the DB accordingly. I created the code in the cshtml file with razor-notation for loop, which means that my input elements all have the same name (to be called in the controller).

I'm trying to figure out how to single out a specific row and get a specific element, even though there are multiple elements with the same name (number of rows in my table).

snippet of my view:

@{
    int i=0;
    foreach(link l in Model.links)
     {
        <tr>
           <td><input class="idField" name="link.Id" type="text" value="@l.Id" disabled="disabled" /></td>
           <td><input class="linkField" name="link.link" type="text" value="@l.link" disabled="disabled" /></td>
           <td><input class="timeField" name="link.Time" type="text" value="@l.Id" disabled="disabled" /></td>
           <td>
              <input class="edit" type="button" value="Edit" formaction="Update" onclick="enableField(@i)" />
           </td>
        </tr>
        i++;
     }
}

Update action in my controller:

public ActionResut Update(){
    LinkDal  dal = new LinkDal(); /*LinkDal uses Entity framework to connect to my DB*/
    List<Link> links = dal.links.ToList<Link>();
    Link toUpdate = dal.links.Find(int.Parse(Request.Form["link.Id"].ToString()));
    toUpdate.link = Request.Form["link.link"].ToString();
    toUpdate.Time = int.Parse(Request.Form["link.link"].ToString());
    dal.SaveChanges();
    VMLinks vml = new VMLinks();
    vml.link = toUpdate;
    vml.links = links;
    return View("MyView", vml);
}

instead of gettin the specific "link" object, "toUpdate" becomes null because the Request.Form doesn't return anything. I tried typing an ID that I know it exists and the code works (the DB is updated). My problem is that I'm not being able to access the text inside the input element because there are multiple elements with the same name (for example - "link.Id").

Gabriel Edery
  • 91
  • 1
  • 8
  • 1
    Please refrain from using `foreach` loop to create input elements with same ID and name, they just create invalid HTML elements. Second, instead of using `Request.Form` collection, use a collection viewmodel property (e.g. `List`) and bound it to dynamically generated elements. Third, `disabled` element will not posted to the controller, use `readonly` to allow submitting values. – Tetsuya Yamamoto Feb 14 '19 at 09:44
  • @TetsuyaYamamoto the "disabled" property is changed later in a script (see the enableField function). At first I enable the element to be edited, and after I'm done - I call the Update action. Regarding the for loop, how else can I create a dynamic-length table otherwise? And last, I don't understand what's wrong with the Request.Form nor what I'm supposed to do instead – Gabriel Edery Feb 14 '19 at 09:58
  • if you don't understand why not to use Request.Form then [take the Microsoft MVC introductory tutorial](https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/getting-started) which explains about binding your posted data to a ViewModel object. Specifically see [this page](https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/examining-the-edit-methods-and-edit-view) in the tutorial. You used a ViewModel object to populate the view, you can also use an object to get the data back again in the C#. – ADyson Feb 14 '19 at 10:20
  • Regarding the posting back of a list of objects, [this answer](https://stackoverflow.com/questions/16321736/how-can-i-post-a-list-of-items-in-mvc) and many similar ones might help you get the concept. Again, it relies on you binding your submitted form data to a suitable viewmodel object. – ADyson Feb 14 '19 at 10:22

0 Answers0