在asp.net MVC中的下拉列表选择值更改中向url字符串发送值

时间:2015-10-03 04:13:43

标签: c# jquery asp.net-mvc visual-studio razor

我有两张桌子。首先是开发区域,第二个是区域。 Zone已将RegionID作为外键。我想填充Zone表中与从下拉列表中选择的Region相关的所有行。我无法弄清楚为什么值没有在url字符串中传递。请帮助我并建议完成它的最佳方法。以下是模型,控制器和视图。

模型区

#include <iostream>
#include <cmath> 

using namespace std;

int main() {
    // your code goes here
    int n = 10;
    int a[n];

    for(int j = 0; j < n; j++) {
       a[j] = n - abs(j - (n / 2)); 
    }

    for(int x = 0; x < n; x++) {
        for(int y = 0; y < (n - a[x]); y++) {
            std::cout << " " << std::endl;
        }
    }

    for(int k = 0; k < a[n-1]; k++) {
        std::cout << " * " << std::endl;
    }

    std::cout << "i" << std::endl;

    return 0;
}

Model DevRegions

public class Zone
{
    [Key]
    public int ZoneID { get; set; }
    [Required]
    [Display(Name = "Zone Code")]
    [RegularExpression(@"^[a-zA-Z]*$"), StringLength(5, ErrorMessage = "Code cannot be more than 5 charachter long")]
    [Column("ZCode")]
    public string ZoneCode { get; set; }
    [Display(Name ="Zone"),RegularExpression(@"^[A-Z]+[a-z]*$"),Required]
    public string ZoneName { get; set; }
    public int RegionID { get; set; }
    public virtual DevRegion devregion { get; set; }
    [Required]
    [Display(Name ="Active")]
    public Boolean isActive { get; set; }
}

ZonesController

public class DevRegion
{
    [Key]
    public int RegionID { get; set; }
    [Required]
    [Display(Name = "Code")]
    [RegularExpression(@"^[a-zA-Z]*$"), StringLength(5, ErrorMessage = "Code cannot be more than 5 charachter long")]
    [Column("RCode")]
    public string RegionCode { get; set; }
    [Required]
    [Display(Name ="Region")]
    [Column("RName")]
    [RegularExpression(@"^[A-Z]+[a-zA-Z\s-]*$", ErrorMessage ="Region can only consist of alphabets, space and dash")]
    [StringLength(30,ErrorMessage ="Region cannot exceed 30 characters")]
    public string RegionName { get; set; }
    [Required]
    [Display(Name ="Active")]
    public Boolean  isActive { get; set; }
}

Index.cshtml

public class ZonesController : Controller
{
    private HuRISContext db = new HuRISContext();

    // GET: Zones
    public ActionResult Index(int? id)
    {
        ViewBag.RegionID = new SelectList(db.DevRegions, "RegionID", "RegionName");
        var zones = db.Zones.Include(z => z.devregion).Where(x=>x.RegionID==(int)(id??x.RegionID));
        return View(zones.ToList());
    }

JQuery的

@model IEnumerable<HuRIS.Models.Zone>
....
<p>@Html.ActionLink("Create New", "Create")</p>
@using (Html.BeginForm("Index","Zones",FormMethod.Get))
{
    @Html.AntiForgeryToken();
    <div class="panel panel-info">
        <div class="panel-body">
            <div class="form-group center-block">
                <label for="RegionID" class="control-label">Region:</label>
                @Html.DropDownList("RegionID", null, "Show all Zones", htmlAttributes: new { @class = "form-control", @onchange = "this.form.submit();" })
            </div>
        </div>
    </div>
}

<table class="table">
    <tr>
        <th>@Html.DisplayNameFor(model => model.devregion.RegionName)</th>
        <th>@Html.DisplayNameFor(model => model.ZoneCode)</th>
        <th>@Html.DisplayNameFor(model => model.ZoneName)</th>
        <th>@Html.DisplayNameFor(model => model.isActive)</th>
        <th></th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@Html.DisplayFor(modelItem => item.devregion.RegionName</td>
            <td>@Html.DisplayFor(modelItem => item.ZoneCode)</td>
            <td>@Html.DisplayFor(modelItem => item.ZoneName)</td>
            <td>@Html.DisplayFor(modelItem => item.isActive)</td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.ZoneID }) |
                @Html.ActionLink("Details", "Details", new { id = item.ZoneID }) | @Html.ActionLink("Delete", "Delete", new { id = item.ZoneID })
            </td>
        </tr>
    }
</table>

1 个答案:

答案 0 :(得分:0)

索引方法如下图所示

public ActionResult Index()
    {
        ViewBag.Region = new SelectList(db.DevRegions, "RegionID", "RegionName");
        var allzones = db.Zones.Include(z => z.devregion);
        return View(allzones.ToList());
    }

创建如下所示的新方法,以接受在下拉列表更改事件中选择的ID,并根据下拉列表中选择的内容创建部分视图以加载表中的数据列表:

public ActionResult ZoneList(int? id)
    {
        var zones = db.Zones.Include(z => z.devregion).Where(x => x.RegionID == (int)(id ?? x.RegionID));
        return PartialView("~/Views/PartialViews/_ZoneList.cshtml", zones.ToList());
    }

更改了javascript如下。

 $("#Region").change(function () {

    var selectedID = $(this).val();

    $.get('/Zones/ZoneList/' + selectedID, function (data) {
        $('.table').html(data);
        //$('.table').fadeOut("linear");
        //$('.table').fadeIn("linear");
    });
});
});
index.cshtml上的

更改了代码如下:

@model IEnumerable<HuRIS.Models.Zone>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken();
<div class="panel panel-info">
    <div class="panel-body">
        <div class="form-group center-block">
            <label for="RegionID" class="control-label">Region:</label>
            @Html.DropDownList("Region", null, "Show all Zones", htmlAttributes: new { @class = "form-control"})


        </div>
    </div>
</div>
}

@{ 
Html.RenderPartial("~/Views/PartialViews/_ZoneList.cshtml", Model);
}

部分视图_ZoneList.cshtml

@model IEnumerable<HuRIS.Models.Zone>

<table class="table">

    <tr>
    <th>
        @Html.DisplayNameFor(model => model.devregion.RegionName)

    </th>
    <th>
        @Html.DisplayNameFor(model => model.ZoneCode)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ZoneName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.isActive)
    </th>
    <th></th>
</tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.devregion.RegionName)

        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ZoneCode)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ZoneName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.isActive)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.ZoneID }) |
            @Html.ActionLink("Details", "Details", new { id = item.ZoneID }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.ZoneID })
        </td>
    </tr>
}