i have this view model:
public class ItemMenuViewModel
{
public ItemMenu Item { get; set; }
public IEnumerable<SelectListItem> Pages { get; set; }
}
for this view:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SIIMVC.Controllers.ItemMenuViewModel>" %>
<%@ Import Namespace="SIIMVC.Models.Extensions" %>
<% using (Ajax.BeginForm("SaveMenuItem",, new { menuItemID = Model.Item.ItemMenuID },
new AjaxOptions { UpdateTargetId = "submitMsg", HttpMethod = "POST", OnSuccess = "MsgUserAndReloadPage" }))
{ %>
<%: Html.ValidationSummary(true)%>
<% Html.EnableClientValidation(); %>
<fieldset class="allRight">
<legend>עריכת פריט בתפריט</legend>
מספר פריט:
<%: Model.Item.ItemMenuID%>
<br />
<div class="editor-label">
שם פריט:
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Item.Text)%>
<%: Html.ValidationMessageFor(model => model.Item.Text,"yoooooooo")%>
</div>
<div class="editor-label">
קישור לדף:
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.Item.PageURL,Model.Pages,"ללא קישור")%>
<%: Html.ValidationMessageFor(model => model.Item.PageURL)%>
</div>
<%-- <%: Html.DropDownList("PageURL",Html.GetPagesSelectList(Model),"ללא קישור"); %>--%>
<div class="editor-label">
מיקום הפריט בתפריט.<br />
נא לציין מספר פריט-הורה:<br />
(לקטגוריה ראשית להשאיר ריק)
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Item.ParentID)%>
<%: Html.ValidationMessageFor(model => model.Item.ParentID)%>
</div>
<div class="editor-label">
סידור הפריט (ביחס לפריטים אחרים):
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Item.OrderNumber)%>
<%: Html.ValidationMessageFor(model => model.Item.OrderNumber)%>
</div><br />
<%if (HttpContext.Current.User.IsInRole("Site Admin"))
{ %>
שייך עמוד לקטגוריה <br />
<%= Html.DropDownListFor(model => model.Item.CategoryID, new SelectList(Html.GetCategories(), "CategoryID", "CategoryName", Model.Item.CategoryID))%>
<%} %><br /><br />
<p>
<input type="submit" value="שמור שינויים" id="saveItem" /> <input type="button" value="מחק פריט" id="deleteItem" />
</p>
<% } %>
<p id="submitMsg">
</p>
</fieldset>
and this is the action that gets the form:
//
// POST: /Admin/SaveMenuItem/34
[HttpPost]
public ActionResult SaveMenuItem(int? menuItemID,FormCollection values)
{
if (!ModelState.IsValid)
return Content("טעות בהכנסת נתונים");
MenuModel menu = new MenuModel();
var item = menu.GetItemByID(menuItemID ?? -1);
UpdateModel(item);
//item.PageURL = db.GetPageByID(item.+".html";
bool success = db.SaveItemMenuToDB(item, false);
if (success)
{
menu.ReloadCache();
return Content("הפריט נשמר בהצלחה");
}
else
return Content(db.UserMessage);
}
( Sorry for the hebrew messages :-D )
the problem: if I look in the formCollection values i do see the form details, but for some reason the update model method doesnt work. i've looked in the modelstate keys and i see that it got only the route value i passed (menuItemID). but why? doesnt it suppose to get all the form collection and update it?
i tried also passing the id in an hidden field so that modelstate was all empty...
Any Ideas ?
UPDATE:
I changed this line: UpdateModel(item);
to this:
item.CategoryID =Convert.ToInt32( Request.Form["Item.CategoryID"] );
item.OrderNumber = Convert.ToInt32(Request.Form["Item.OrderNumber"]);
item.ParentID = Convert.ToInt32(values["Item.ParentID"]);
item.Text = Request.Form["Item.Text"];
item.PageURL = Request.Form["Item.PageURL"];
Now i know this is not the way to do it right, but meanwhile it works... does anyone know a way to solve this so it will also be a best practice ? Thanks!