简体   繁体   English

如何在 ASP.NET MVC 的编辑视图中的下拉列表中填充值

[英]How to fill value in dropdownlist in edit view in ASP.NET MVC

I have a form which contains a list of country and state name with edit and delete option.我有一个包含国家列表和 state 名称的表单,其中包含编辑和删除选项。

Here is my model and controller这是我的 model 和 controller

Model Model

public class StateModel
{
    public int StateID { get; set; }
    [Required]
    public int CountryID { get; set; }
    [DisplayName("State Name")]
    [Required]
    public string StateName { get; set; }
    public string CrDate { get; set; }
    public string CrBy { get; set; }
    public string mdfDate { get; set; }
    public string mdfBy { get; set; }
    public string isAct { get; set; }
    public string isDelete { get; set; }
}

public class StateDisplayModel // Index Listing
{
    [DisplayName("#")]
    public int StateID { get; set; }
    [DisplayName("Country")]
    public string Countryname { get; set; }
    [DisplayName("State")]
    public string StateName { get; set; }
}

Controller Controller

    public ActionResult UpdateStates(int Id)
    {           
        StateData statData = new StateData();

        List<StateDisplayModel> stateList = statData.stateDisplaytList();
        var cntry = stateList.Where(s => s.StateID == Id).FirstOrDefault();

        return View(cntry);
    }

    [HttpPost]
    public ActionResult UpdateStates(StateModel _stsMdl)
    {          
        if (_stsMdl.StateName != "" && _stsMdl.CountryID > 0)
        {
            StateData mystData = new StateData();
            string rs = mystData.updateState(_stsMdl);

            if (rs == "1")
            {
                return RedirectToAction("States");
            }
            else
            {
                return Content(rs.ToString());
            }
        }
        else
        {
            return View();
        }
    }

I want to display the selected country name in dropdownlist while edit but I don't get my view properly我想在编辑时在下拉列表中显示选定的国家/地区名称,但我的视图不正确

View看法

 @model IEnumerable<Intrans.Core.SuperAdmin.StateDisplayModel>
 @Html.DropDownListFor(m => m.stateId, mcntry)
                    @Html.ValidationMessageFor(model => model.stateId, "", new { @class = "text-danger" })

How to fix this?如何解决这个问题?

Thanks in advance.提前致谢。

https://dotnetfiddle.net/88K0av https://dotnetfiddle.net/88K0av

Controller/Action/View Model控制器/动作/视图 Model

namespace WebApplicationSkip1.Controllers
{
    public class CountryModel
    {
        [DisplayName("#")]
        public int StateID { get; set; }
        [DisplayName("Country")]
        public string Countryname { get; set; }
        [DisplayName("State")]
        public string StateName { get; set; }
    }

    public class StateDisplayModel // Index Listing
    {
        public List<CountryModel> CountryModel { get; set; }
        public string SelectedCountry { get; set; }
    }

    public class StateData
    {
        public StateDisplayModel stateDisplaytList()
        {
            CountryModel sdm1 = new CountryModel { Countryname = "USA", StateID = 1, StateName = "Arizona" };
            CountryModel sdm2 = new CountryModel { Countryname = "France", StateID = 2, StateName = "FranceCity" };
            CountryModel sdm3 = new CountryModel { Countryname = "Germany", StateID = 3, StateName = "GermanyCity" };
            StateDisplayModel sdm = new StateDisplayModel();
            sdm.CountryModel = new List<CountryModel>
            {
                sdm1,
                sdm2,
                sdm3
            };
            return sdm;
        }
    }

    public class HomeController : Controller
    {
        [HttpPost]
        public ActionResult Index8(StateDisplayModel sdm)
        {
            //put a breakpoint here to see selected item
            var q = sdm.SelectedCountry;
            return View();
        }

        public ActionResult Index8(int? Id)
        {
            //initial id
            Id = 1;

            StateData statData = new StateData();

            StateDisplayModel cntry = statData.stateDisplaytList();
            //var cntry = stateList.Where(s => s.StateID == Id).FirstOrDefault();
            //get all instead

            return View(cntry);
        }

View看法

@model WebApplicationSkip1.Controllers.StateDisplayModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index8</title>
</head>
<body>
    <div>
        @using (Html.BeginForm())
        {
            @*@Html.DropDownListFor(m => m.stateId, mcntry)*@
            @Html.DropDownListFor(r => r.SelectedCountry, new SelectList(Model.CountryModel, "Countryname", "Countryname"), "--select--")
            @*@Html.ValidationMessageFor(model => model.stateId, "", new { @class = "text-danger" })*@
            <input type="submit" value="Go" />
        }

    </div>
</body>
</html>

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

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