简体   繁体   English

模型未在MVC asp.net C#中传递值

[英]Model is not passing value in MVC asp.net C#

I am having basic problem in passing value at View through @Model, but unfortunately it is even not offering option for that. 我在通过@Model传递View的值时遇到基本问题,但不幸的是,它甚至没有为此提供选项。 Data is not passing when I am hitting BACK from cities to states, it must bring me back to the states of that country, not all states. 当我从城市回击到各州时,数据没有传递,它必须使我回到那个国家的州,而不是所有州。

Here is Controller 这是管制员

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);
    }
}
}

Here are the Models 这是模型

Country Model 国家模型

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; }

}
}

State Model 状态模型

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; }
    }
}

City Model 城市模型

    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; }
    }
}

Here are Views. 这是视图。

Country View 国家景观

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

State View 状态视图

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

View of Cities 城市景观

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

Problem 问题

You're not able to retrieve the country with the model you've passed to the city-view. 您无法使用传递到城市视图的模型来检索国家/地区。 The only thing you have is an id of the state, not the full state object. 您唯一拥有的是状态的ID,而不是完整的状态对象。 Therefor you don't have the country id. 因此,您没有国家/地区编号。

Solution

Write a helper-class. 编写一个助手类。 The helper class could look like this. 助手类可能看起来像这样。

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;
    }
}

That's very abstract and no error-handling implemented. 那是非常抽象的,没有实现错误处理。 You should do that by yourself. 你应该自己做。

After that you can update your view. 之后,您可以更新视图。

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

Foreach foreign-key-relationship you can do the same. 对于Foreach外键关系,您可以执行相同操作。

You View is IEnumerable strongly type view. 您的视图是IEnumerable强类型视图。 And on controller side your are accepting only single item. 而在控制器方面,您只接受单个项目。

Use this 用这个

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

Instead of 代替

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM