2012-04-30 36 views
0

我想要做的是创建一个SubCategory对象。为了做到这一点,我做了一个视图模型,它将为我的视图提供nescesarry数据(包括子类别将绑定到的类别对象)。ViewModel在表单提交时为空

当我发布我的表单时,viewmodel返回给我的控制器,但是我的子类别的所有属性和从下拉列表中选择的值都是空的。

我在做什么错? :S

视图:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<SkyLearn.Areas.Categories.Models.CategoryViewModel>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    Create 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

<h2>Create</h2> 

<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script> 
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script> 

<form action="" method="post" enctype="multipart/form-data"> 
    <%: Html.ValidationSummary(true) %> 
    <fieldset> 
     <legend>SubCategory</legend> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.subcategory.Title) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.EditorFor(model => model.subcategory.Title)%> 
      <%: Html.ValidationMessageFor(model => model.subcategory.Title)%> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.subcategory.Icon)%> 
     </div> 
     <div class="editor-field"> 
      <input type="file" name="icon" id="icon"/> 
     </div> 

     <%: Html.DropDownListFor(selectedcategory => Model.selectedCategory, Model.categories) %> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.subcategory.Description)%> 
     </div> 
     <div class="editor-field"> 
      <%: Html.EditorFor(model => model.subcategory.Description)%> 
      <%: Html.ValidationMessageFor(model => model.subcategory.Description)%> 
     </div> 

     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
</form> 

<div> 
    <%: Html.ActionLink("Back to List", "Index") %> 
</div> 

</asp:Content> 

<asp:Content ID="Content3" ContentPlaceHolderID="SideContent" runat="server"> 
</asp:Content> 

控制器:

[Authorize(Roles = "administrator")] 
     [HttpPost] 
     public ActionResult Create(CategoryViewModel viewmodel, HttpPostedFileBase Icon) 
     { 
      SubCategory subcategory = viewmodel.subcategory; 

      subcategory.Category = categorycontroller.getCategoryByName(viewmodel.selectedCategory); 

      if (Icon != null && Icon.ContentLength > 0) 
      { 
       // extract only the filename 
       var fileName = Path.GetFileName(Icon.FileName); 
       // store the file inside ~/App_Data/uploads folder 
       var path = Path.Combine(Server.MapPath("../../Content/icons/"), fileName); 
       Icon.SaveAs(path); 
       subcategory.Icon = fileName; 
      } 

      if (ModelState.IsValid) 
      { 
       db.subcategories.Add(subcategory); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      return View(subcategory); 
     } 

视图模型:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Data.Entity; 
using System.Web.Mvc; 

namespace SkyLearn.Areas.Categories.Models 
{ 
    public class CategoryViewModel 
    { 
     public List<SelectListItem> categories; 
     public SubCategory subcategory; 
     public string selectedCategory; 

     public CategoryViewModel() 
     { 
      categories = new List<SelectListItem>(); 
      subcategory = new SubCategory(); 
      selectedCategory = ""; 
     } 
    } 
} 

的视图模型包含了子类别即时试图创建可结合类别的列表至。它还包含我可以用来创建子类别的子类别对象。而最后一个属性是我想用来绑定下拉列表中的选择的字符串。

+0

可能是该帖子:http://stackoverflow.com/questions/9513385/creating-a-dropdown-in-mvc3-c-sharp-with-viewmodel-and-easy-model-binding-on-pos/ 9513747#9513747,帮助您获得默认的模型绑定。 –

+0

我已阅读了关于该主题的大部分帖子。我可能是错的,但我想我已经有输入字段(html.editorfor),并使用正确的名称来绑定它。林有点新的mvc,所以我可能是完全错误 – AronChan

+0

再次,我必须承认,我忘了一些简单的东西,如添加获取;组;现在一切正常。叹。感谢您的帮助 – AronChan

回答

1

ASP.Net MVC3的SelectListItem不符合您的期望。另外,请尝试使用Html.DropDownListFor()而不是Html.EditorFor()来创建您的下拉列表。

在视图模型:

public IList<string> PossibleValues {get; set;} 
public string SelectedValue {get; set;} 

装入值放入您的视图模型的构造PossibleValues。

在查看:

@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.PossibleValues)) 

这将自动生成你的下拉列表并绑定到你的模型。如果需要,您也可以将默认值和其他自定义项传递到此Html帮助程序功能中。

保存其他值 您可以保存其他值,这对用户不应该有编辑的选择,但其值不是关键,如果用户不编辑,与

@Html.HiddenFor(m => m.RememberThisValue); 

请记住,该值对于显示而言是隐藏的,但仍然可以在DOM上进行编辑并随用户需要发布。确保通过检查POST'd隐藏值来防止恶意注入。

存储服务器端重要的所有内容,通过模型将哈希/私钥传递给用户,并实现静态会话字典以存储这些小部分信息。

+0

我使用Html.DropDownListFor。 dropdownlist助手接受的唯一来源是selectItem列表。所以这会导致错误。有没有其他方法可以将我的其余属性与模型绑定? – AronChan

+0

我设法让选定的值在提交时发布,这要感谢您的回复。对我来说,我仍然是一个谜,我可以如何从视图中获得我的viewmodel的其余部分,并填入正确的值。对此有何评论? – AronChan

+0

是的,你是对的。我修正了我的例子,并添加了关于其他值的答案。 – Khelvaster