2012-04-30 15 views
0

私がしようとしているのは、サブカテゴリオブジェクトを作成することです。これを行うために、サブセルトリーデータ(サブカテゴリがバインドされるカテゴリオブジェクトを含む)をビューに提供するビューモデルを作成しました。フォーム提出時にViewModelがnullです

ビューポーモデルはコントローラに返されますが、私のサブカテゴリとドロップダウンリストから選択した値のプロパティはすべてnullです。

何が間違っていますか? :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); 
     } 

のViewModel:

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 = ""; 
     } 
    } 
} 

のviewmodelを作成しようとサブカテゴリイムがバウンドすることができカテゴリのリストが含まれていますに。また、サブカテゴリを作成するために使用できるサブカテゴリオブジェクトも含まれています。最後のプロパティは、ドロップダウンリストの選択肢をバインドするために使用したい文字列です。

+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)を持っていて、それをバインドするために正しい名前を使用していると思います。私は完全に間違っている可能性がありますので、私はまったく間違っているかもしれません。 – AronChan

+0

もう一度、私はGetを追加するだけの簡単なことを忘れてしまったことを恥ずらしく認めなければなりません。セット;それはすべて今働く。一息。ヘルプのおかげで – AronChan

答えて

1

ASP.Net MVC3のSelectListItemは、どうやって期待通りに動作しません。また、Html.EditorFor()ではなく、Html.DropDownListFor()を試してドロップダウンリストを作成してください。 ViewModelには

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

あなたのViewModelのコンストラクタでPossibleValuesに値をロードします。ビューで

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

これは、あなたのドロップダウンリストを自動生成し、モデルにバインドします。必要に応じて、このHTMLヘルパー関数にデフォルト値やその他のカスタマイズを渡すこともできます。

保存その他はあなたが

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

で、ユーザーが編集するオプションを持つべきではありませんが、ユーザが編集を行う場合には、ミッションクリティカルなされていない他の値、値を保存することができます 値この値は表示されないようになっていますが、DOM上で編集して、ユーザーが望むものを投稿することができます。あなたが悪意のあるバリューインジェクションから身を守るためには、POSTされた隠し値をチェックしてください。

重要なことはすべてサーバー側に保存し、モデルを通じてハッシュ/秘密鍵をユーザーに渡し、静的なセッション辞書を実装してこれらの小さな情報を格納します。

+0

私はHtml.DropDownListForを使用しています。 また、dropdownlistヘルパーが受け入れる唯一のソースはselectItem listです。そのため、エラーが発生します。他のプロパティをモデルにバインドする方法はありますか? – AronChan

+0

私はあなたの返信のおかげでsubmitに投稿されるselectedvalueを得ることができました。ビューの正しい値が表示されたビューモデルの残りの部分をどうやって得ることができるのかはまだ分かりません。それに関するコメント? – AronChan

+0

ええ、そうです。私は私の例を修正し、あなたの価値の残りの部分について答えを加えました。 – Khelvaster

関連する問題