SubCategory 개체를 만들려고합니다. 이렇게하려면 하위 범주 데이터가 포함 된 뷰 모델을 만들 것입니다 (하위 범주가 바인딩되는 범주 개체 포함).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 = "";
}
}
}
에. 하위 카테고리를 만들 때 사용할 수있는 하위 카테고리 개체도 포함되어 있습니다. 마지막 속성은 드롭 다운 목록에서 선택 항목을 바인딩하는 데 사용하려는 문자열입니다.
5 월 게시물 일 수 있습니다. http://stackoverflow.com/questions/9513385/creating-a-dropdown-in-mvc3-c-sharp-with-viewmodel-and-easy-model-binding-on-pos/ 9513747 # 9513747, 기본 모델 바인딩에 도움이됩니다. –
나는이 주제에 대한 대부분의 게시물을 이미 읽었습니다. 내가 틀렸을 수도 있지만 이미 입력 필드 (html.editorfor) 및 올바른 이름을 바인딩하는 데 사용하는 것 같아요. 내가 완전히 틀릴 수도 있기 때문에 mvc에 다소 새로운 Im – AronChan
다시 한번 나는 내가 Get를 추가하는 것만 큼 간단하게 잊어 버렸음을 부끄럽게 인정해야한다. 세트; 이제는 모두 작동합니다. 한숨.도움을 주셔서 감사합니다 – AronChan