My view is populated with model properties and some of the drop list are hardcoded and are passed to view through viewdata, The issue is Trupdatemodel only updates the model values , I cant update the viewdata values , Below is the code which updates the model but not viedata dropdown list :
View:
<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>
Controller:
[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"]开发者_StackOverflow中文版 = 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");
}
}
You need to have the data you want updated in a model for TryUpdateModel
to update those values. If you just put them in ViewData, the data will only be passed to the view and there is no way for the controller to update those values using TryUpdateModel
.
You need to include the select list data that is currently in your ViewData into the Event
object or create an object that will include the values you want updated. Then using TryUpdateModel
will update that data with the posted values. When you create this new object or add it to Event
, strongly type your view to this object so you will not need to do ViewData casts in the View.
精彩评论