Мое представление заполнено свойствами модели, а некоторые из выпадающего списка жестко закодированы и переданы для просмотра через viewdata. Проблема заключается в том, что Trupdatemodel обновляет только значения модели, я не могу обновить значения viewdata. Ниже приведен код, который обновляет выпадающий список модели, но не viedata:
Вид:
<div class="editor-field">
<% if (ViewBag.mode != "edit")
{ %>
<%: Html.TextBoxFor(model => model.Date)%>
<%}else{%>
<%: Html.TextBoxFor(model => model.Date, new { @Value = Model.Date.ToShortDateString() })%>
<%} %>
<%: Html.ValidationMessageFor(model => model.Date) %>
</div></td></tr><tr><td>Experience level</td><td><div class="editor-field">
<%: Html.DropDownListFor(model => model.ExperienceLevelID, (SelectList)ViewData["Experience"], "--select--")%>
<%: Html.ValidationMessageFor(model => model.ExperienceLevelID) %>
</div></td></tr>
<tr><td>No of Attendees</td><td><div class="editor-field">
<%-- <%: Html.DropDownList("attendees1", (SelectList)ViewData["attendees1"], "--select--")%>--%>
<% if (ViewBag.mode != "edit")
{ %>
<%: Html.DropDownList("attendees1", ViewData["attendees1"] as SelectList, "--select--")%>
<%}else{%>
<%: Html.DropDownList("attendees1")%>
<%} %>
<label>£</label> <%: Html.TextBox("attendeeCost", ViewData["txtAttendees"], new { maxlength = "5", style = "width:40px;" })%> <label>Price/Unit</label>
</div></td></tr>
<tr><td>Second Driver</td><td> <div class="editor-field">
<label>£</label> <%: Html.TextBoxFor(model => model.SecondDriverPrice, new { maxlength = "5", style = "width:40px;" })%> <label>Price</label>
<%: Html.CheckBoxFor(model => model.SecondDriver) %><label>Free</label>
</div></td></tr>
<tr><td>No of Helmets</td><td><div class="editor-field">
<% if (ViewBag.mode != "edit")
{ %>
<%: Html.DropDownList("helmets", ViewData["size"] as SelectList, "--select--")%>
<%}else{%>
<%: Html.DropDownList("helmets", "--select--")%>
<%} %>
<label>£</label> <%: Html.TextBox("unitCosthelmet", ViewData["txtHelmet"], new { maxlength = "5", style = "width:40px;" })%> <label>Price/Unit</label>
</div></td></tr>
Контроллер:
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
ViewBag.mode = "edit";
// for dropdown track
ITrackRepository trackResp = new TrackRepository();
IQueryable<Object> tracks = trackResp.GetVenuesSelectlist();
ViewData["Venue"] = new SelectList(tracks, "VenueID", "Name");
// for dropdown for event type
ITrackdayRepository trackdayResp = new TrackdayRepository();
IQueryable<EventType> eventTypes = trackdayResp.GetAllEventTypes();
ViewData["EventTypes"] = new SelectList(eventTypes, "ID", "Name");
// for dropdown experience
IExperienceLevelRepository expLevelResp = new ExperienceLevelRepository();
IQueryable<ExperienceLevel> expLevel = expLevelResp.GetAllExperienceLevels().OrderBy(ExperienceLevel => ExperienceLevel.Name);
ViewData["Experience"] = new SelectList(expLevel, "ID", "Name");
// dropdown for helmets
IProductRepository helmetResp = new ProductRepository();
Product productHelmet = helmetResp.GetProd(id);
var attendeesList = Enumerable.Range(1, 80).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
ViewData["attendees1"] = new SelectList(attendeesList.ToList(), "Value", "Text");
// dropdown for helmets
var helmetsList = Enumerable.Range(1, 200).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
ViewData["helmets"] = new SelectList(helmetsList.ToList(), "Value", "Text");
//ViewBag.helmet = new SelectList(helmets.ToList(), "Value", "Text", product.QtyAvailable);
// dropdown for garages
var garagesList = Enumerable.Range(1, 50).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
ViewData["garages"] = new SelectList(garagesList.ToList(), "Value", "Text");
Event trackday = trackdayResp.GetEvent(id);
TryUpdateModel(trackday, collection);
if (ModelState.IsValid)
{
// product.DateModified = DateTime.Now;
trackday.DateModified = DateTime.Now;
TempData["msg"] = trackdayResp.Save().ToString();
helmetResp.Save();
return RedirectToAction("Index");
}
else
{
return View("Create");
}
}