C# 模型或控制器中的Asp.net mvc下拉代码

C# 模型或控制器中的Asp.net mvc下拉代码,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我已经创建了ASP.NETMVC4Web应用程序,它有注册页面。它包含两个下拉项 我想知道在哪里可以放置填充下拉项的代码。目前,它位于Registration controller中,但由于服务器端验证,我在每个方法中编写了两次相同的代码。 注册索引 public ActionResult Index() { var model = new RegisterModel { TitleList = new SelectList(DataAcce

我已经创建了ASP.NETMVC4Web应用程序,它有注册页面。它包含两个下拉项

我想知道在哪里可以放置填充下拉项的代码。目前,它位于Registration controller中,但由于服务器端验证,我在每个方法中编写了两次相同的代码。

注册索引

    public ActionResult Index()
    {
      var model = new RegisterModel
      {
       TitleList = new SelectList(DataAccess.DAL.DropDownValues("Title"), "Value", "Text"),
       BankList = new SelectList(DataAccess.DAL.DropDownValues("Bank"), "Value", "Text")
      };
     return View("Register", model);
    }
登记后行动

    [HttpPost]
    [ActionName("doRegister")]
    public ActionResult doRegister([Bind(Exclude = "TitleList")] RegisterModel registerModel)
    {
        var model = (RegisterModel)null;
        if (ModelState.IsValid)
        {
            if (DataAccess.DAL.registerCustomer(registerModel))
            {
                return RedirectToAction("Index", "Login");
            }
        }
        else
        {
            model = new RegisterModel
            {
             TitleList = new SelectList(DataAccess.DAL.DropDownValues("Title"), "Value", "Text"),
             BankList = new SelectList(DataAccess.DAL.DropDownValues("Bank"), "Value", "Text")
            };
        }
        return View("Register", model);
    }
若我并没有用下拉值初始化模型对象,它会在我的视图中抛出错误

@Html.DropDownListFor(m => m.Title, new SelectList(Model.TitleList, "Value", "Text"),
 "Select Title", new { @class = "form-control" })

我想知道在哪里可以放置下拉代码?

您可以在控制器中创建函数,并从两个位置调用它。我看不出有什么问题。此外,您正在创建SelectList的实例,我认为这属于控制器的责任

public RegisterModel FillModel()
{
    return new RegisterModel
            {
                TitleList = new SelectList(DataAccess.DAL.DropDownValues("Title"), "Value", "Text"),
                BankList = new SelectList(DataAccess.DAL.DropDownValues("Bank"), "Value", "Text")
            };
}

您可以在控制器中创建函数,并从两个点调用它。我看不出有什么问题。此外,您正在创建SelectList的实例,我认为这属于控制器的责任

public RegisterModel FillModel()
{
    return new RegisterModel
            {
                TitleList = new SelectList(DataAccess.DAL.DropDownValues("Title"), "Value", "Text"),
                BankList = new SelectList(DataAccess.DAL.DropDownValues("Bank"), "Value", "Text")
            };
}
为什么不使用:

@Html.DropDownListFor(m => m.Title, Model.TitleList,
"Select Title", new { @class = "form-control" })
因为Model.TitleList的定义是一个
SelectList

编辑 您可以使用
if
子句包围
DropdownList代码
,并检查模型是否为
null
或否,然后继续。 最好将代码放在控制器中,而不是视图中。视图必须仅在用户界面中显示模型的显示。对于控制器来说,它实际上是MVC的核心,MVC是将模型和视图联系在一起的中介,也就是说,它接受用户输入,操纵模型并使视图更新,为什么不使用:

@Html.DropDownListFor(m => m.Title, Model.TitleList,
"Select Title", new { @class = "form-control" })
因为Model.TitleList的定义是一个
SelectList

编辑 您可以使用
if
子句包围
DropdownList代码
,并检查模型是否为
null
或否,然后继续。
最好将代码放在控制器中,而不是视图中。视图必须仅在用户界面中显示模型的显示。对于控制器来说,它实际上是MVC的核心,MVC是将模型和视图联系在一起的中介,也就是说,它接受用户输入,操纵模型,并使视图更新

在这两个操作中调用的新方法如何?比如说
CreateRegistrationModel
我可以在模型中编写代码来填充下拉列表吗?在两个操作中调用的新方法如何?比如说
CreateRegistrationModel
我可以在模型中编写代码来填充下拉列表吗?您可能是指
返回新的…
在模型中编写相同的代码是否不好?然后在每个控制器动作中调用model方法?你的方法应该返回一个
RegisterModel
my bad我的意思是返回new.@SagarDumbre你也可以在模型中写。。。这比您的模型将使用引用来访问SelectList类要简单,因此我不会在模型中这样做。您可能的意思是
返回新的…
在模型中编写相同的内容是否不好?然后在每个控制器动作中调用model方法?你的方法应该返回一个
RegisterModel
my bad我的意思是返回new.@SagarDumbre你也可以在模型中写。。。这比您的模型使用引用访问SelectList类要简单得多,所以我不会因为这个原因在模型中这样做。问题是:我想知道我可以在哪里放置下拉代码?问题是:我想知道我可以在哪里放置下拉代码?