简体   繁体   中英

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

You View is IEnumerable strongly type view. 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 }) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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