I view list data and I want to edit some field example as image, list view data, and edit it
i have a field (ex:choosed_value with some value 1,2,3,4,5) in view, i use radiobutton to get value from choosed_value, but when i submit to save, i can't get value choosed from radiobutton, field 'content' saved ok, field 'choosed_value' not save.
my code
in view
view radiobutton
@for (int j = 1; j <= 5; j++)
{
<td>
@Html.RadioButton(Model[i].ID.ToString(), Model[i].choosed_value,Model[i].choosed_value == j) ? true : false )
</td>
}
if choosed_value = 1 then radiobutton with value = 1, checked = true
in controller
[HttpPost]
public ActionResult Index(List<Content> contents)
{
DEMOEntities DbContent = new DEMOEntities();
foreach (Content cont in contents)
{
Content Existed_Cont = DbContent.Contents.Find(cont.ID);
Existed_Cont.Content = cont.Content;
Existed_Cont.choosed_value = cont.choosed_value;
}
DbContent.SaveChanges();
}
please help me ...
I think your radiobutton names are incorrect (first param of Html.RadioButton). The result input name in browser should not be ID, it should be like [0].choosed_value for correct model binding on POST.
@for (int j = 1; j <= 5; j++)
{
<td>
@Html.RadioButtonFor(x => Model[i].choosed_value, ...)
</td>
}
instead of
@for (int j = 1; j <= 5; j++)
{
<td>
@Html.RadioButton(Model[i].ID.ToString(), Model[i].choosed_value,Model[i].choosed_value == j) ? true : false )
</td>
}
Use the browser dev-tools to see what is actually passed as data to your controller. There should be some fields with the name 'contents' or the model-binder cannot pick them up.
First of all we shouldnt make a database call from action method directly. Now your ViewModel's list should be of type List<SelectListItem>
, in that way you can get the "selected" item's value straight forward.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.