2

所以我正在构建一个简单的 mvc4 应用程序,我已经创建了用于创建数据库的所有基本模型,从这些类中我可以自然地创建具有匹配视图的基本控制器。

现在我的问题:我有基本的 create actionresult + 视图,在这个视图中,我希望用户能够从下拉列表中选择一些值,这将使新对象的创建更简单。

如果我想使用这些下拉菜单(它们相互过滤(所以首先用户必须指定一个大陆,然后是国家只显示来自该大陆的国家,并且在他指定一个国家之后,区域下拉列表会更新:) )) 基本视图的提交总是被自动调用。

所以让下拉列表自己更新不是问题:s 是创建的表单会在下拉列表更新时自动验证

这是下拉菜单相互过滤的控制器

//
// GET: /FederationCenter/Create
public ActionResult Create(string searchRegion, string searchCountry, string searchContinent)
{
  var countries = from c in db.Countries select c;
  if (!String.IsNullOrEmpty(searchContinent))
  {
    Continent searchContinentEnumValue = (Continent)Enum.Parse(typeof(Continent), searchContinent);
    countries = from c in db.Countries where ((int)c.Continent).Equals((int)searchContinentEnumValue) select c;
  }

  var regions = from r in db.Regions where r.Country.Name.Equals(searchCountry) select r;

  ViewBag.searchContinent = new SelectList(Enum.GetNames(typeof(SchoolCup.Domain.Continent)));
  ViewBag.searchCountry = new SelectList(countries, "Name", "Name");
  ViewBag.searchRegion = new SelectList(regions, "Name", "Name");
  return View();
}

//
// POST: /FederationCenter/Create

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(NSSF nssf, string searchRegion, string searchCountry, string searchContinent)
{
  var countries = from c in db.Countries select c;
  if (!String.IsNullOrEmpty(searchContinent))
  {
    Continent searchContinentEnumValue = (Continent)Enum.Parse(typeof(Continent), searchContinent);
    countries = from c in db.Countries where ((int)c.Continent).Equals((int)searchContinentEnumValue) select c;
  }

  var regions = from r in db.Regions where r.Country.Name.Equals(searchCountry) select r;

  ViewBag.searchContinent = new SelectList(Enum.GetNames(typeof(SchoolCup.Domain.Continent)));
  ViewBag.searchCountry = new SelectList(countries, "Name", "Name");
  ViewBag.searchRegion = new SelectList(regions, "Name", "Name");
  if (ModelState.IsValid)
  {
    var naam = Request["searchRegion"];
    Region creatie = (from c in db.Regions where c.Name.Equals(naam) select c).SingleOrDefault();
    nssf.ISFId = 1;
    nssf.RegionId = creatie.RegionId;
    db.NSSFs.Add(nssf);
    db.SaveChanges();
    return RedirectToAction("Index");
  }
  return View(nssf);
}

这是我的观点

@model SchoolCup.Domain.POCO.NSSF

@{
ViewBag.Title = "Create";
}

<h2>Create NSSF</h2>
     <div>
        @using (Html.BeginForm(null, null, FormMethod.Post, new { name = "form" }))
        {
         @Html.AntiForgeryToken()

        @Html.DropDownList("searchContinent", null, "-- All continents --", new { onchange = "sendForm()" }) 
        @Html.DropDownList("searchCountry", null, "-- All countries --", new { onchange = "sendForm()" })
        @Html.DropDownList("searchRegion", null, "-- All regions --", new { onchange = "sendForm()" })
            <>
            <input type="submit" name= value="Search" />    
        }
    </div>   
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>NSSF</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>

更多输入

   </fieldset>
    <p>
        <input type="submit" value="Create" />
        @Html.ActionLink("Back to List", "Index", null, new { @class = "button" })

    </p>
}

@section Scripts {
<script type="text/javascript">
    function sendForm() {
        document.form.submit()
    }
    </script>
}

我一直在寻找至少一天,我不知道如何解决这个问题

关于亚历山大

4

1 回答 1

2

或者(1)使用 JQuery 并使用控制器返回的 Partial 视图加载下拉列表,或者(2)您可以有一个 AJAX 调用,它将您的值作为从您的实体映射的 JSON 对象返回,您可以渲染它们在下拉菜单中。这样,每次更新下拉列表时都不会提交表单。

第一个解决方案可能如下所示:

查询

<script>
$("#searchContinent").change(function() { 
    $("#searchCountry").load("/YourController/GetCountries", { 'continentId': $(this).val() },
                                        function (response, status, xhr) {
                                            if (status == "error") {
                                                alert("An error occurred while loading the results.");
                                            }
                                        });
});
</script>

@Html.DropDownList("searchContinent", null, "-- All continents --" }) 
<div id="searchCountry">
    <!--the newly created drop-down will be placed here-->
</div>

(编辑)

对于 Javascript,您可以尝试以下操作:

您当前的视图

@Html.DropDownList("searchContinent", null, "-- All continents --", new { onchange = "getCountries(this)" }) 
<div id="searchCountry">
<!--the newly created drop-down will be placed here-->
</div>

<script> 
function getCountries(input){
    var selectedValue = input.options[input.selectedIndex].value;
    var xhReq = new XMLHttpRequest();
    xhReq.open("GET", "YourController/GetCountries?continentId="+selectedValue, false);
    xhReq.send(null);
    var serverResponse = xhReq.responseText;
    document.getElementById("searchCountry").innerHTML=serverResponse ;
}
</script>

免责声明:我从未尝试过,所以如果有错误,请随时让我知道并在必要时更正

(结束编辑)


控制器

public ActionResult GetCountries(string continentId)
    {
        /*Get your countries with your continentId and return a Partial View with a list of 
          countries as your model*/


        return PartialView(countryList);
    }

GetCountries 查看

@model IEnumerable<SchoolCup.Domain.Country>

@Html.DropDownListFor( 0, Model)
于 2013-05-02T22:23:39.040 回答