为什么我的模型后失去DropDownList的项目? [英] Why my model in post lose dropdownlist items?

查看:139
本文介绍了为什么我的模型后失去DropDownList的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动作一个简单的页面MVC。
在索引操作我创建属性model.categoria的实例获取方法,我和3项值。
现在的问题是,如果我跑行动的岗位指标,因为你可以看到下面,我有一个错误,因为DropDownList的相关model.categoria为空。
我的问题是:
我总是在实例化每一个岗位的model.categoria为了避免这个问题,一方面,今天我不知道,是理所当然的?

下面是code,我将返回一个错误:
值不能为空。

控制器动作:

 公众的ActionResult指数()
{
    VAR模型=新Models.UploadVideoPage.UploadVideoPageModel();
    model.categoria =新的List< Models.UploadVideoPage.ListCategorie>();
    model.categoria.Add(新ListCategorie {TitoloCategoria =Volontariato,idCategoria = 1});
    model.categoria.Add(新ListCategorie {TitoloCategoria =INTERNO,idCategoria = 2});
    model.categoria.Add(新ListCategorie {TitoloCategoria =Sindacato,idCategoria = 3});
    返回查看(模型);
}
[HttpPost]
公众的ActionResult指数(Models.UploadVideoPage.UploadVideoPageModel模型)
{
    返回查看(模型);
}

index.cshtml

  @ Html.LabelFor(X => x.categoria)
            @ Html.DropDownListFor(X =方式> x.categoria.First()idCategoria,新的SelectList(Model.categoriaidCategoria,TitoloCategoria))


解决方案

您不要在每次做一回发,除非你把隐藏字段的所有值表单中的时间来填充列表。只需要code几行,虽然使这项工作以不同的方式

 公众的ActionResult指数()
    {
        VAR模型=新Models.UploadVideoPage.UploadVideoPageModel();
        PopulateDependencies(模型);
        返回查看(模型);
    }
    [HttpPost]
    公众的ActionResult指数(Models.UploadVideoPage.UploadVideoPageModel模型)
    {
        PopulateDependencies(模型);
        返回查看(模型);
    }    私人无效PopulateDependencies(Models.UploadVideoPage.UploadVideoPageModel模型)
    {
        model.categoria =新的List< Models.UploadVideoPage.ListCategorie>();
        model.categoria.Add(新ListCategorie {TitoloCategoria =Volontariato,idCategoria = 1});
        model.categoria.Add(新ListCategorie {TitoloCategoria =INTERNO,idCategoria = 2});
        model.categoria.Add(新ListCategorie {TitoloCategoria =Sindacato,idCategoria = 3});
    }

I have one simple page mvc with one action. Get method in the index action I create an instance of the property model.categoria and I value with 3 items. The problem is that if I run the post index of action, as you can see below, I have an error because the dropdownlist associated model.categoria is null. My question is: I always instantiated at every post the model.categoria order to avoid this problem, one that today I wonder, is rightly so?

Here is the code that I will return an error: Value cannot be null.

controller action:

public ActionResult Index()
{
    var model = new Models.UploadVideoPage.UploadVideoPageModel();
    model.categoria = new List<Models.UploadVideoPage.ListCategorie>();
    model.categoria.Add(new ListCategorie { TitoloCategoria = "Volontariato", idCategoria = 1 });
    model.categoria.Add(new ListCategorie { TitoloCategoria = "Interno", idCategoria = 2 });
    model.categoria.Add(new ListCategorie { TitoloCategoria = "Sindacato", idCategoria = 3 });
    return View(model);
}
[HttpPost]
public ActionResult Index(Models.UploadVideoPage.UploadVideoPageModel model) 
{
    return View(model);
}

index.cshtml

 @Html.LabelFor(x => x.categoria)
            @Html.DropDownListFor(x => x.categoria.First().idCategoria, new SelectList(Model.categoria, "idCategoria", "TitoloCategoria"))

解决方案

You do have to populate the list every time you do a post back unless you were to put hidden field for all of the values in your form. It only takes a few lines of code though to make this work a different way

public ActionResult Index()
    {
        var model = new Models.UploadVideoPage.UploadVideoPageModel();
        PopulateDependencies(model);
        return View(model);
    }
    [HttpPost]
    public ActionResult Index(Models.UploadVideoPage.UploadVideoPageModel model)
    {
        PopulateDependencies(model);
        return View(model);
    }

    private void PopulateDependencies(Models.UploadVideoPage.UploadVideoPageModel model)
    {
        model.categoria = new List<Models.UploadVideoPage.ListCategorie>();
        model.categoria.Add(new ListCategorie { TitoloCategoria = "Volontariato", idCategoria = 1 });
        model.categoria.Add(new ListCategorie { TitoloCategoria = "Interno", idCategoria = 2 });
        model.categoria.Add(new ListCategorie { TitoloCategoria = "Sindacato", idCategoria = 3 });
    }

这篇关于为什么我的模型后失去DropDownList的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆