Pass value from editor for method

I have EditorFor in my view. Like this

 @Html.EditorFor(model => model.First().Link, new { htmlAttributes = new { @class = "form-control", placeholder = "Email", id= "start" } }) 

Also I am an Action in the controller, which finds all NULL in the table from the database and updates it with some value, here is the code

  public ActionResult Update(string start="lol") { ApplicationDbContext context = new ApplicationDbContext(); IEnumerable<InvitationMails> customers = context.InvitationMails .Where(c => c.Link == null) .AsEnumerable() .Select(c => { c.Link = start; return c; }); foreach (InvitationMails customer in customers) { // Set that row is changed context.Entry(customer).State = EntityState.Modified; } context.SaveChanges(); return RedirectToAction("Index"); } 

In the "View Indexes" window, I click the "Update Action" button and run it. Here is the code

 <ul class="btn btn-default" style="width: 150px; list-style-type: none; font-size: 16px; margin-left: 20px"> <li style="color: white">@Html.ActionLink(" ", "Update", "InvitationMails", null, new { @style = "color:white" })</li> </ul> 

But here is an update with a static value, and I want to get the value from VIew. How can I write my code?

+6
source share
1 answer

Basically you should set the name attribute of the displayed input .

If you are using MVC 4, you have EditorFor spatial overload for this case.

You can use it as follows:

 @Html.EditorFor(model => model.First().Link, null, "start", //That will set id and name attributes to start new { @class = "form-control", placeholder = "Email" }) 

Note that you no longer need id="start" .

After your update:

Basically you have 2 options.

1s - use form :

 @using (Html.BeginForm("Update", "InvitationMails", FormMethod.Post)) //maby you need GET { @Html.EditorFor(model => model.First().Link, null, "start", //That will set id and name attributes to start new { @class = "form-control", placeholder = "Email" }) <ul class="btn btn-default" style="width: 150px; list-style-type: none; font-size: 16px; margin-left: 20px"> <li style="color: white"> <button type="submit" style="color:white"> </button> </li> </ul> } 

The second option is to use js to link to the action link.

+1
source

Source: https://habr.com/ru/post/1015647/


All Articles