C# 从ASP MVC中的DropDownList中获取所选项目

C# 从ASP MVC中的DropDownList中获取所选项目,c#,asp.net-mvc,asp.net-mvc-2,C#,Asp.net Mvc,Asp.net Mvc 2,控制器 public ActionResult Create() { return View(new PageViewModel()); } [HttpPost] public ActionResult Create(Page page) { try { repPage.Add(page); repPage.Save(); return RedirectToAction("Edit"); } catch {

控制器

public ActionResult Create()
{
   return View(new PageViewModel());
} 

[HttpPost]
public ActionResult Create(Page page)
{
   try
   {
      repPage.Add(page);
      repPage.Save();

      return RedirectToAction("Edit");
   }
   catch
   {
      return View(new PageViewModel());
   }
}
PageViewModel

public class PageViewModel
{
    public Page Page { get; set; }
    public List<Template> Templates { get; set; }

    private TemplateRepository repTemplates = new TemplateRepository();

    public PageViewModel()
    {
        Page = new Page();
        Templates = repTemplates.GetAllTemplates().ToList(); 
    }
}
公共类页面视图模型
{
公共页页面{get;set;}
公共列表模板{get;set;}
private TemplateRepository repTemplates=new TemplateRepository();
公共页面视图模型()
{
页面=新页面();
Templates=repTemplates.GetAllTemplates().ToList();
}
}
我观点的一部分

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Website.Models.PageViewModel>" %>

<%= Html.TextBoxFor(model => model.Page.Name, new { @style = "width:300px;" })%>

<%= Html.DropDownListFor(model => model.Page.Template, new SelectList(Model.Templates, "ID", "Name"), new { @style = "width:306px;" })%>

model.Page.Name,新的{@style=“width:300px;”})%%>
model.Page.Template,新的选择列表(model.Templates,“ID”,“Name”),新的{@style=“width:306px;”})%%>
模板: 身份证件 名称
页面: 身份证件 名称 模板体

我的dropdownlist在视图中正确填充,没有问题。我的问题是我没有从dropdownlist中获取所选的值

在我的控制器中,我在
Edit
中放置了一个断点,并看到
Name
文本框中填充了我输入的值。但从dropdownlist中选择的值设置为null


如果我遗漏了什么,我认为应该在
页面
对象中设置正确的
模板
值。我做错了什么?

试试这样的吧? 集合中的键是dropdownlist控件的名称

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(FormCollection collection)
        {
           try
           {
              string selectedvalue = collection["Template"];

              return RedirectToAction("Edit");
           }
           catch
           {
              return View(new PageViewModel());
           }
        }

试试这样吧? 集合中的键是dropdownlist控件的名称

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(FormCollection collection)
        {
           try
           {
              string selectedvalue = collection["Template"];

              return RedirectToAction("Edit");
           }
           catch
           {
              return View(new PageViewModel());
           }
        }

从web页面返回的唯一内容是所选模板的id。默认模型绑定器不知道如何获取此id并从存储库中检索正确的模板对象。为此,您需要实现一个自定义模型绑定器,该绑定器能够从数据库中检索值、构造模板对象并将其分配给页面。或您可以在操作中手动执行此操作,因为根据您在其他地方的评论,您知道如何从发布的值中获取模板的id。

从网页中获取的唯一内容是所选模板的id。默认模型绑定器不知道如何获取此id并从存储库中检索正确的模板对象。为此,您需要实现一个自定义模型绑定器,该绑定器能够从数据库中检索值、构造模板对象并将其分配给页面。或您可以在操作中手动执行此操作,因为根据您在其他地方的评论,您知道如何从发布的值中获取模板的id。

好的,我喜欢这样:

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                Page page = new Page();
                page.Name = collection["Page.Name"];
                page.TemplateID = int.Parse(collection["Page.Template"]);
                page.Created = DateTime.Now;

                repPage.Add(page);
                repPage.Save();

                return RedirectToAction("Edit");
            }
            catch
            {
                return View(new PageViewModel());
            }
        }
它工作得很好,我只是想知道是否有更好的方法来实现这一点,而无需手动从集合中获取值

但我想如果没有像tvanfosson说的那样制作自己的模型活页夹,这是不可能的

谢谢大家。

好的,我喜欢这样:

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                Page page = new Page();
                page.Name = collection["Page.Name"];
                page.TemplateID = int.Parse(collection["Page.Template"]);
                page.Created = DateTime.Now;

                repPage.Add(page);
                repPage.Save();

                return RedirectToAction("Edit");
            }
            catch
            {
                return View(new PageViewModel());
            }
        }
它工作得很好,我只是想知道是否有更好的方法来实现这一点,而无需手动从集合中获取值

但我想如果没有像tvanfosson说的那样制作自己的模型活页夹,这是不可能的


谢谢大家。

对于模型绑定,请在ActionResult中使用:

partial ActionResult(FormCollection form)
{
    Page page = new Page();
    UpdateModel(page);
    return view(page);
}
你们班:

public class Page
{
  public string Name {get; set;}
  public int Template {get; set;}
  public DateTime Created {get; set;}

  public Page()
  {
    this.Created = DateTime.Now;
  }

}

类中的属性名称应等于视图字段的名称

对于模型绑定,请在ActionResult中使用:

partial ActionResult(FormCollection form)
{
    Page page = new Page();
    UpdateModel(page);
    return view(page);
}
你们班:

public class Page
{
  public string Name {get; set;}
  public int Template {get; set;}
  public DateTime Created {get; set;}

  public Page()
  {
    this.Created = DateTime.Now;
  }

}

类中的属性名称应等于视图字段的名称

int-templateID=int.Parse(集合[“Page.Template”])此代码在编写时正确返回模板id。但是是否可以从强类型对象获取值,而不是从
FormCollection
中写入“Page.Template”?
int-templateID=int.Parse(collection[“Page.Template”])此代码在编写时正确返回模板id。但是,是否可以从强类型对象而不是从
FormCollection
中获取值并编写“Page.Template”?如果使用默认的MVC模型绑定器,这肯定会很好。如果有人找到更清洁的解决方案,请插嘴!使用默认的MVC模型绑定器实现这一点当然很好。如果有人找到更清洁的解决方案,请插嘴!