级联DropdownList与MVC剃刀实体框架工作 [英] Cascading DropdownList With MVC razor Entity frame work

查看:57
本文介绍了级联DropdownList与MVC剃刀实体框架工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果任何人有解决方案如何使用实体框架制作带MVC剃刀的Cascade下拉列表,请给我示例代码或更正我的代码。我的代码是



控制器:



公共ActionResult索引(字符串MatchTypeName,字符串CompetitionName,字符串MatchName, string GroundName,string ReportName)

{

foreach(db.WebReportTypes中的WebReportType WebRepots)

{

SelectListItem selectReportType = new SelectListItem

{

Text = WebRepots.ReportName,

Value = WebRepots.ReportName,

< br $>
};



SelectReports.Add(selectReportType);



ViewBag。 ReportName = SelectReports;

}



foreach(db.MatchTypeMasters中的MatchTypeMaster MatchType)

{

SelectListItem selectMatchTypes = new SelectListItem

{

Text = MatchType.MatchTypeName,

Value = MatchType.MatchTypeID,



};



selectMatchTypeList.Add(selectMatchTypes);



ViewBag.MatchTypeName = selectMatchTypeList;

}



foreach(db.Competitions中的竞争CompMatchType)

{



SelectListItem selectCompetitionTypes = new SelectListItem

{

Text = CompMatchType.CompetitionName,

Value = CompMatchType.CompetitionID,



};



selectCompetitionList.Add(selectCompetitionTypes);

ViewBag.CompetitionName = selectCompetitionList;

}



foreach(db.Matches中的匹配)

{



SelectListItem selectMatch = new SelectListItem

{

Text = Match.MatchName,

Value = Match.MatchID,



};



selectMatchList.Add(selectMatch);

ViewBag.MatchName = selectMatchList;

}



foreach(db.Grounds中的地面)

{



SelectListItem selectGround = new SelectListItem

{

Text = Ground.GroundName,

Value = Ground.GroundID,



};



selectGroundList.Add(selectGround);

ViewBag.GroundName = selectGroundList;

}



查看:





@ Html.DropDownList(ReportName,ViewData [ReportName]为MultiSelectList,new {multiple =multiple,@ class =multiselect,style =width:400px})< br $>


@ Html.DropDownList(MatchName,ViewData [MatchName]为MultiSelectList,new {multiple =multiple,@ class =multiselect,style = width:400px})



@ Html.DropDownList(MatchTypeName,ViewData [MatchTypeName]为MultiSelectList,new {multiple =multiple,@ class =multiselect})

解决方案

我已经给了我的代码试试吧... 

在视图文件夹中 Index.cshtml

@ {
ViewBag.Title =DropDownList Demo;
}

< h5 > 级联DropDownList演示< / h5 >

@ Html.ValidationSummary(请更正错误,然后重试。)

@using(Html.BeginForm( ))
{
< fieldset >
< 图例 > DropDownList < / legend >
@ Html.Label(名称)
@ Html.TextBox(名称)
@ Html.ValidationMessage(名称,* )

@ Html.Label(State)
@ Html.DropDownList( State,ViewBag.StateName as SelectList,选择一个州,新{id =State})
@ Html.ValidationMessage(State,*)

@ Html.Label(区)
< 选择 id = 名称 = > < / select >
@Html。 ValidationMessage(District,*)

< p < span class =code-keyword>>
< 输入 类型 = 提交 value = 创建 id = SubmitId / >
< / p >
< / fieldset >
}


@ Scripts.Render(〜/ bundles / jquery)
< script type = text / jscript >


function (){


' #State')。change( function (){

Please give me sample code or correct my code if any one have solution for how to make Cascade dropdown list with MVC razor by using entity framework. My code is

Controller:

public ActionResult Index(string MatchTypeName, string CompetitionName, string MatchName, string GroundName, string ReportName)
{
foreach (WebReportType WebRepots in db.WebReportTypes)
{
SelectListItem selectReportType = new SelectListItem
{
Text = WebRepots.ReportName,
Value = WebRepots.ReportName,

};

SelectReports.Add(selectReportType);

ViewBag.ReportName = SelectReports;
}

foreach (MatchTypeMaster MatchType in db.MatchTypeMasters)
{
SelectListItem selectMatchTypes = new SelectListItem
{
Text = MatchType.MatchTypeName,
Value = MatchType.MatchTypeID,

};

selectMatchTypeList.Add(selectMatchTypes);

ViewBag.MatchTypeName = selectMatchTypeList;
}

foreach (Competition CompMatchType in db.Competitions)
{

SelectListItem selectCompetitionTypes = new SelectListItem
{
Text = CompMatchType.CompetitionName,
Value = CompMatchType.CompetitionID,

};

selectCompetitionList.Add(selectCompetitionTypes);
ViewBag.CompetitionName = selectCompetitionList;
}

foreach (Match Match in db.Matches)
{

SelectListItem selectMatch = new SelectListItem
{
Text = Match.MatchName,
Value = Match.MatchID,

};

selectMatchList.Add(selectMatch);
ViewBag.MatchName = selectMatchList;
}

foreach (Ground Ground in db.Grounds)
{

SelectListItem selectGround = new SelectListItem
{
Text = Ground.GroundName,
Value = Ground.GroundID,

};

selectGroundList.Add(selectGround);
ViewBag.GroundName = selectGroundList;
}

View:


@Html.DropDownList("ReportName", ViewData["ReportName"] as MultiSelectList, new { multiple = "multiple", @class = "multiselect" , style = "width: 400px"})

@Html.DropDownList("MatchName", ViewData["MatchName"] as MultiSelectList, new { multiple = "multiple", @class = "multiselect" , style = "width: 400px"})

@Html.DropDownList("MatchTypeName", ViewData["MatchTypeName"] as MultiSelectList, new { multiple = "multiple", @class = "multiselect" })

解决方案

i have given my code just try it...

in view folder  Index.cshtml

@{
    ViewBag.Title = "DropDownList Demo";
}

<h5>Cascade DropDownList Demo</h5>

@Html.ValidationSummary("Please correct the errors and try again.")

@using (Html.BeginForm())
{
    <fieldset>
        <legend>DropDownList</legend>
        @Html.Label("Name")
        @Html.TextBox("Name")
        @Html.ValidationMessage("Name", "*")

        @Html.Label("State")
        @Html.DropDownList("State", ViewBag.StateName as SelectList, "Select a State", new { id = "State" })
        @Html.ValidationMessage("State", "*")

        @Html.Label("District")
        <select id="District"  name="District"></select>
        @Html.ValidationMessage("District", "*")

        <p>
            <input type="submit" value="Create" id="SubmitId" />
        </p>
    </fieldset>
}


@Scripts.Render("~/bundles/jquery")
<script type="text/jscript">


(function () {


('#State').change(function () {


这篇关于级联DropdownList与MVC剃刀实体框架工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆