Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何使用<;正确实现下拉框;选择列表项>;ASP.NET MVC中的数据库和存储库模式_Asp.net Mvc_Asp.net Mvc 3_Entity Framework_Razor_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 如何使用<;正确实现下拉框;选择列表项>;ASP.NET MVC中的数据库和存储库模式

Asp.net mvc 如何使用<;正确实现下拉框;选择列表项>;ASP.NET MVC中的数据库和存储库模式,asp.net-mvc,asp.net-mvc-3,entity-framework,razor,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 3,Entity Framework,Razor,Asp.net Mvc 4,我想知道如何使用ASP.NET MVC中的类型正确实现下拉框。我以前从未使用过这种方法,而且有人告诉我,如果我必须对下拉列表执行“必需”验证,那么这是最好的方法 我以前使用ViewBag创建了一个下拉列表,我认为这种方法不是很有效 我这里的例子很简单。一个小应用程序,允许用户输入客户名称并从下拉列表中选择国家。它还应该检查用户是否选择了一个国家,并在名称文本框中输入了一个值。请看下面我的代码。它是不完整的,只是一个模板,所以请帮助我如何完全实现这种方法。如果使用存储库是有效的,请随意使用它。我仍

我想知道如何使用ASP.NET MVC中的
类型正确实现下拉框。我以前从未使用过这种方法,而且有人告诉我,如果我必须对下拉列表执行“必需”验证,那么这是最好的方法

我以前使用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; } 
}
客户视图 该视图显示一个简单界面,用于输入客户名称并从dorp下拉列表中选择国家。我想应该是下面的那种

@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)

将选择列表包括在您的
客户中

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; } 
}
你的观点

@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)
然后在您的post方法中

  • 确保模型有效(服务器端验证)
  • 将模型映射回实体
  • 将实体保存到数据库中

  • 基本上就是这样,您只需要用特定于您的项目的代码填充空格。

    谢谢。我现在就试试看。只有一个问题,“CountryId”属性是否仍然必须是“Country”类型?如果是,我应该在“国家”类中拥有哪些属性?另外,您会推荐automapper作为映射工具吗?很抱歉,它应该是int,或者是
    Id
    的类型。我现在正尝试使用您建议的方法创建下拉列表。我还没有使用这个存储库,因为我仍然在绞尽脑汁想它。我直接访问上下文并填充视图模型“Customer”。当我试图将countryname从数据库映射到'Countries'属性时,我得到了错误'cannot convert to string to',您能帮忙吗?如果需要,我可以发布我的数据访问代码。感谢这样的方法:
    var items=newlist();添加(新SelectListItem{Text=country.Id,Value=country.Name})其中
    country
    是循环中使用的变量。您还可以使用linq生成SelectListItems,这有几种方法。
    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)