我应该从控制器传递给我的ASP.NET MVC 4实体框架的看法? [英] What should I pass from the controller to my view in ASP.NET MVC 4 with Entity Framework?

查看:111
本文介绍了我应该从控制器传递给我的ASP.NET MVC 4实体框架的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的ASP.NET MVC,请您帮我,而不是批评的问题。
我有其中有4类名为登记表格式国家状态城市。最后三个是在我的表格中的级联DROPDOWNLIST

 公共部分类的形式
{
        公众诠释标识{搞定;组; }
        公共字符串唯一ID {搞定;组; }
        公共字符串国家{搞定;组; }
        公共字符串状态{搞定;组; }
        公共字符串城市{搞定;组; }
        公众可空<长>引脚code {搞定;组; }
        公众可空<长>移动电话号码{搞定;组; }
        公共字符串EMAILID {搞定;组; }        有些领域......        公共虚拟国家的国家{搞定;组; }
        公共虚拟州州{搞定;组; }
        公共虚拟城市城市{搞定;组; }
}

最后三个虚拟财产我已经添加到类

该国类有:

 公众诠释CountryId {搞定;组; }
公共字符串国家或地区名称{搞定;组; }
公共虚拟的ICollection< tbTehsil>美国{搞定;组; }

国家和市级也有类似的领域。

现在我想从我区级获得的所有值和填充我DDL。

这code是在Home控制器具有指针的操作方法

  FarmerDbContext DB =新FarmerDbContext();
清单<国家>国家= db.Country.ToList();
返回查看(国家);

在我的 Index.cshtml 我强烈窗体类键入它

  @model IEnumerable的< test.Models.form>@ {
    ViewBag.Title =指数;
}< H2>指数< / H>@ Html.DropDownListFor(X => x.countries,新的SelectList(Model.countries,CountryId,CountyName),----选择国家-----)

但它在我的DropDownList给出了一个错误,当我删除的IEnumerable<> 从该视图顶部它提供了另一个错误是相当明显的。


  

传递到字典的模型项的类型是System.Collections.Generic.List`1 [test.Models.country]',但本词典需要类型的test.Models.form的典范项目。



解决方案

我不认为你真的需要表单对象为您Index.cshtml集合。你应该只创建一个表单对象,然后你需要,然后将它传递给你的模型的所有信息进行初始化。

 公共类HomeController的:控制器
{
    公众的ActionResult指数()
    {
        VAR形式=新test.Models.form();
        FarmerDbContext DB =新FarmerDbContext();
        form.Countries = db.Country.ToList();
        返回查看(表);
    }
}

Index.cshtml:

  @model test.Models.form
@ {
    ViewBag.Title =指数;
}< H2>指数< / H>
@ Html.DropDownListFor(X => x.countries,新的SelectList(Model.countriesCountryId,国家或地区名称),-----选择国家-----)

I am new to ASP.NET MVC and please help me rather than criticizing the question. I have a registration form which has 4 classes named form, country, state, city. The last three are for populating the cascading Dropdownlist in my form

public partial class form
{
        public int Id { get; set; }
        public string UniqueId { get; set; }
        public string Country{ get; set; }
        public string state { get; set; }
        public string city { get; set; }
        public Nullable<long> PinCode { get; set; }
        public Nullable<long> MobileNumber { get; set; }
        public string EmailId { get; set; }

        and some fields ......

        public virtual Country countries { get; set; }
        public virtual State states { get; set; }
        public virtual City cities { get; set; }
}

The last three virtual properties I have added to the class

The country class has:

public int CountryId { get; set; }
public string CountryName { get; set; }
public virtual ICollection<tbTehsil> states { get; set; }

The state and city class have similar fields.

Now I want to get all values from my district class and populate my ddl.

This code is in Home controller having index action method

FarmerDbContext db = new FarmerDbContext();
List<Country> country= db.Country.ToList();
return View(country);

In my Index.cshtml I have strongly typed it with form class

@model IEnumerable<test.Models.form>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@Html.DropDownListFor(x => x.countries, new SelectList(Model.countries, "CountryId", "CountyName"),"----select Country -----")

but it gives an error on my dropdownlist and when I remove IEnumerable<> from top of this view it gives another error which is quite obvious.

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[test.Models.country]', but this dictionary requires a model item of type 'test.Models.form'.

解决方案

I don't think you really need a collection of form objects for your Index.cshtml. You should create just one form object and then initialize it with all the information you need and then pass it to your model.

public class HomeController : Controller 
{
    public ActionResult Index()
    {
        var form = new test.Models.form();
        FarmerDbContext db = new FarmerDbContext();
        form.Countries = db.Country.ToList();
        return View(form);
    }
}

Index.cshtml:

@model test.Models.form
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@Html.DropDownListFor(x => x.countries, new SelectList(Model.countries, "CountryId", "CountryName"), "----- Select Country -----")

这篇关于我应该从控制器传递给我的ASP.NET MVC 4实体框架的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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