【发布时间】:2015-07-09 09:24:16
【问题描述】:
我在通过@Model 在 View 传递值时遇到了基本问题,但不幸的是,它甚至没有为此提供选项。当我从城市返回到州时,数据不会传递,它必须将我带回那个国家的州,而不是所有州。
这里是控制器
using MVCDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCDemo.Controllers
{
public class CountryController : Controller
{
public ActionResult GetCountries()
{
DatabaseContext databaseContext = new DatabaseContext();
List<Country> countries = databaseContext.Countries.ToList();
return View(countries);
}
public ActionResult GetStates(int CountryId)
{
DatabaseContext databaseContext = new DatabaseContext();
List<State> states = databaseContext.States.Where(ctry => ctry.CountryId == CountryId).ToList();
return View(states);
}
public ActionResult GetCities(int StateId)
{
DatabaseContext databaseContext = new DatabaseContext();
List<City> cities = databaseContext.Cities.Where(st => st.StateId == StateId).ToList();
return View(cities);
}
}
}
这里是模型
国家模式
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace MVCDemo.Models
{
[Table("Countries")]
public class Country
{
public int CountryId { get; set; }
public string CountryName { get; set; }
}
}
状态模型
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace MVCDemo.Models
{
[Table("States")]
public class State
{
public int StateId { get; set; }
public string StateName { get; set; }
public int CountryId { get; set; }
}
}
城市模型
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace MVCDemo.Models
{
[Table("Cities")]
public class City
{
public int CityId { get; set; }
public string CityName { get; set; }
public int StateId { get; set; }
}
}
这里是视图。
国家视图
@using MVCDemo.Models;
@model IEnumerable<Country>
@{
ViewBag.Title = "GetCountries";
}
<h2>GetCountries</h2>
<ul>
@foreach(Country country in @Model)
{
<li>
@Html.ActionLink(country.CountryName, "GetStates", "Country", new {@CountryId = country.CountryId}, null)
</li>
}
</ul>
状态视图
@using MVCDemo.Models;
@model IEnumerable <State>
@{
ViewBag.Title = "GetStates";
}
<h2>GetStates</h2>
<ul>
@foreach (State state in @Model)
{
<li>
@Html.ActionLink(state.StateName, "GetCities", "Country", new {@StateId = state.StateId}, null)
</li>
}
</ul>
@Html.ActionLink("Back", "GetCountries")
城市景观
@using MVCDemo.Models;
@model IEnumerable <City>
@{
ViewBag.Title = "GetCities";
}
<h2>GetCities</h2>
<ul>
@foreach (City city in @Model)
{
<li>@city.CityName</li>
}
</ul>
//Error comes here in following line. Model is not passing value of CountryId
@Html.ActionLink("Back to States", "GetStates", new { CountryId = @Model.CountryId })
【问题讨论】:
-
什么是由“Back to States”ActionLink 生成的 URL? "Model is not pass value of CountryId"究竟是什么意思?
-
首先,您的 Cities 视图是
IEnumerable<City>,但您使用的不是 IEnumerable 的属性@Model.CountryId。 -
或者:使用 2x 属性(城市列表、countryid)或从 IList
继承并添加 CountryId 创建一个适当的 CitiesViewModel -
@CodeCaster 编译错误。
@Model.CountryId下方的红线 -
@freedomn-m 表有对外关系....州与国家和城市与州,为什么我要在城市表与国家之间添加更多关系...
标签: c# asp.net asp.net-mvc asp.net-mvc-4