如何在ASP.NET MVC中使用<selectlistitem>和存储库模式正确实现下拉框</selectlistitem>

时间:2013-04-09 09:40:13

标签: asp.net-mvc asp.net-mvc-3 entity-framework razor asp.net-mvc-4

我想知道如何在ASP.NET MVC中使用 <SelectListItem> 类型正确实现下拉框。我之前没有使用过这种方法,而且我被告知如果我必须在下拉列表中执行“必需”验证,这是最好的方法。

我之前使用ViewBag创建了一个下拉列表,我觉得我觉得这种方法不是很有效。

我的例子很简单。一个小应用程序,允许用户输入客户名称并从下拉列表中选择一个国家/地区。它还应检查用户是否选择了国家/地区并在名称文本框中输入值。请看下面的代码。它不完整,只是一个模板,所以请帮助我如何全面实现这种方法。如果使用存储库有效,请随意使用存储库。我仍然在努力了解如何使用存储库模式,因此也需要帮助和解释。感谢

客户视图模型

public class Customer
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public Country CountryId { get; set; }
}

public class Country 
{
    public int CountryId { get; set; }
    public IEnumerable<SelectListItem> Countries { get; set; } 
}

型号

  1. 客户 - 包含客户详细信息表的EntityFramework实体,该表包含对国家/地区实体的外键
  2. 国家/地区 - 包含CountryId和CountryName
  3. 表格的实体

    行动结果

    Models.EFEntities ctx = new Models.EFEntities();
    
    public ActionResult GetCustomers()
    {
        using(ctx) 
        {
            Need code to properly implement this part 
        }
            return view("Customers");
    }
    [HttpPost]
    public ActionResult AddCustomer(Model.Customer customer)
    {
        using(ctx) 
        {
            //I'm thinking of calling the SaveChanges() method on Customers Entity, 
            //but please let me know if you have any better ways of writing this code.
            // something like using repository pattern)
        }
        return view();
    }
    

    客户查看 视图显示一个简单的界面,用于输入客户名称并从dorp-down中选择一个国家/地区。我认为它应该像下面那样

    @model Models.Customer
    
    @Html.EditorFor(model=>model.Name)
    @Html.ValidationFor(model=>model.Name)
    @Html.DropDownListFor(model => model.Country..?..Need code here as well, "please select")
    @Html.ValidationFor(...Need code here to make sure the user selects a country)
    

1 个答案:

答案 0 :(得分:1)

Customer

中加入选择列表
public class Customer
{
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public int CountryId { get; set; }

    public IEnumerable<SelectListItem> Countries { get; set; } 
}

在控制器中构建模型,如下所示:

public ActionResult GetCustomers()
{
    var model = new Customer();
    using(ctx) 
    {
        // Need code to properly implement this part 
        // > I assume you know how to do this, something like:
        var customer = ctx.Customers.Get(the_id);
        // map the entity to your model
        model.Id = customer.Id;
        // map the rest of the fields here (you may want to use mapper tools)

        // get the country list from your db and map it to selectlistitem
        model.Countries = mapCountries(ctx.Countries.GetAll());
    }
    return view(model);
}

您的观点

@model Models.Customer

@Html.EditorFor(model=>model.Name)
@Html.ValidationFor(model=>model.Name)
// implement the rest of your fields
@Html.DropDownListFor(model => model.CountryId, Model.Countries, "please select")
@Html.ValidationFor(model => model.CountryId)

然后在您的帖子方法中

  1. 确保模型有效(服务器端验证)
  2. 将您的模型映射回您的实体
  3. 将实体保存到数据库
  4. 基本上就是这样,你只需要用你项目特有的代码填空。

相关问题