【问题标题】:Model is not passing value in MVC asp.net C#模型未在 MVC asp.net C# 中传递值
【发布时间】: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&lt;City&gt;,但您使用的不是 IEnumerable 的属性 @Model.CountryId
  • 或者:使用 2x 属性(城市列表、countryid)或从 IList 继承并添加 CountryId 创建一个适当的 CitiesViewModel
  • @CodeCaster 编译错误。 @Model.CountryId下方的红线
  • @freedomn-m 表有对外关​​系....州与国家和城市与州,为什么我要在城市表与国家之间添加更多关系...

标签: c# asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

您的视图是IEnumerable 强类型视图。在控制器方面,您只接受单个项目。

使用这个

@Html.ActionLink("Back to States", "GetStates", new { CountryId = @Model.FirstOrDefault().CountryId })

代替

@Html.ActionLink("Back to States", "GetStates", new { CountryId = @Model.CountryId }) 

【讨论】:

  • 这不提供@Model.FirstOrDefault().CountryId,因为在当前模型中它只有StateIdCityId。我将不得不使用CountryId 来显示这些城市的同一国家/地区。
【解决方案2】:

问题

您无法使用传递给城市视图的模型检索国家/地区。您唯一拥有的是状态的 id,而不是完整的状态对象。因此,您没有国家/地区 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) })

对于每个外键关系,您也可以这样做。

【讨论】:

  • 与@AjayPunekar 的上述评论相同
  • 啊,现在我知道你想要显示的城市所在国家的所有州?
  • @Html.ActionLink("Back to previous page", null, null, null, new { href = Request.UrlReferrer}) 完成此操作...正常工作
猜你喜欢
  • 2014-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
相关资源
最近更新 更多