Set any external value in a model from view page

I have to set @item.ID in model, as in given code I am setting Amount in EditorFor(model => model.Amount).I have to save that amount and ID in database so both should be post in my action of controller.

@foreach (var item in ViewBag.countries)
{              
  <td><label class="control-label">@item.Name</label></td>                                              
  <td>@Html.EditorFor(model => model.Amount)</td>
  <td><input type="text" /></td>
  <td><button type="submit" class="btn-primary</i>Update</button></td>               
}

My Model is:--

public class AllocationViewModel
    {
        public long ID { get; set; }
        public double Amount { get; set; }
        public long CountryId { get; set; }
    }

Here I have to set countryID in model from item.ID of viewbag.

Answers


Add model.CountryId in a hidden field for each item, so you can post it to server.

@foreach (var item in ViewBag.countries)
{              
  <td><label class="control-label">@item.Name</label></td>                                              
  <td>@Html.EditorFor(model => model.Amount)</td>
  <td>@Html.HiddenFor(model => model.CountryId, new { @Value = item.ID })</td>
  <td><input type="text" /></td>
  <td><button type="submit" class="btn-primary</i>Update</button></td>               
}

Need Your Help

Hyphen within string in mysql query causing strange behavior

mysql sql ruby-on-rails database

In my Rails testing environment, I have a user_id that looks like 1234-567abc89. I'm getting inconsistent behaviour by querying this user in different tables. Most of the queries are working, but r...

Send grepped tail output to netcat

unix grep tail netcat

I am trying to run the following command, and nothing is getting sent to netcat