![](/img/trans.png)
[英]ASP.NET MVC - Passing a Model referencing a different assembly from a Controller to a View using C#
[英]Model is not passing value in MVC asp.net C#
我在通過@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 })
您無法使用傳遞到城市視圖的模型來檢索國家/地區。 您唯一擁有的是狀態的ID,而不是完整的狀態對象。 因此,您沒有國家/地區編號。
編寫一個助手類。 助手類可能看起來像這樣。
class Helper
{
public int GetCountryIdByCity(int cityId)
{
City city = databaseContext.States.Where(cty => cty.CityId == cityId).First();
State state = databaseContext.States.Where(st => st.StateId == city.StateId).First();
return databaseContext.Countries.Where(ctry => ctry.CountryId == state.CountryId).First().CountryId;
}
}
那是非常抽象的,沒有實現錯誤處理。 你應該自己做。
之后,您可以更新視圖。
@Html.ActionLink("Back to States", "GetStates", new { CountryId = @Helper.GetCountryIdByCity(Model.First().CityId) })
對於Foreach外鍵關系,您可以執行相同操作。
您的視圖是IEnumerable
強類型視圖。 而在控制器方面,您只接受單個項目。
用這個
@Html.ActionLink("Back to States", "GetStates", new { CountryId = @Model.FirstOrDefault().CountryId })
代替
@Html.ActionLink("Back to States", "GetStates", new { CountryId = @Model.CountryId })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.