English 中文(简体)
名单BoxFor not binding my viewmodel
原标题:ListBoxFor not binding my viewmodel

我知道,有人在SO问题上问得很麻烦。

here and here

但是,我仍然能够指出这一问题。

我正在研制一种博客,以教授自己性别与发展框架。 现在,当我发表以下看法时,名单BoxFor helper对我的模式没有任何约束。 清单成功地包含了所有类别,但当名册管制人员撤回了意见模式时,类别物体即告无效。

此处为《示范法》:

public class PostViewModel
{
    public Post Posts { get; set; }
    public IEnumerable<Category> Categories { get; set; }
}

主计长:

    public ActionResult Create()
    {
        PostViewModel post = new PostViewModel();
        post.Categories = db.ListCategories();
        return View(post);
    }

观点:

<p>@Html.ListBoxFor(model => model.Categories, new MultiSelectList(Model.Categories, "CategoryID", "CategoryName"))</p>
最佳回答

我认为,你应当拥有一套物色财产,这些物色识别器将对其具有约束力。

public class PostViewModel
{
    public Post Posts { get; set; }
    public int[] SelectedCategoryIds { get; set; }
    public IEnumerable<Category> Categories { get; set; }
}

修改 电话: SelectedCategoryIds property.

<p>@Html.ListBoxFor(model => model.SelectedCategoryIds, new MultiSelectList(Model.Categories, "CategoryID", "CategoryName"))</p>

页: 1 选定的不动产,如果您对清单贴上标签,则应将其改为<代码>。 SelectedCategoryIds property also.

@Html.LabelFor(model => model.SelectedCategoryIds, "Categories")

(Categories” is thebell text)

问题回答

如果我能理解你的问题,那么这部法律是否有助于? 它表明,在将表格贴回服务器时,你可以选择哪些类别。

[HttpPost]
public ActionResult Create(Post post, FormCollection formCollection) 
{
  var listOfCategoryIDs = formCollection["categories"];
  var arrayOfCategoryIDs = listOfCategoryIDs.Split( , );
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签