Asp.net mvc 4 Mvc4在一个视图中有两个窗体

Asp.net mvc 4 Mvc4在一个视图中有两个窗体,asp.net-mvc-4,Asp.net Mvc 4,因此,我正在构建一个简单的mvc4应用程序,我已经创建了用于创建DB的所有基本模型,从这些类中,我可以自然地创建具有匹配视图的基本控制器。 现在我的问题是:我有一个基本的createactionresult+视图,在这个视图中,我希望用户能够从dropdownlist中选择一些值,这将简化新对象的创建。 我尝试用第二个表单来实现这一点在我看来第一个表单是基本的创建表单现在如果我想使用这些下拉列表,它们会互相过滤,所以首先用户必须指定一个大陆,然后,国家下拉列表只显示该大陆的国家,在他指定一个国家

因此,我正在构建一个简单的mvc4应用程序,我已经创建了用于创建DB的所有基本模型,从这些类中,我可以自然地创建具有匹配视图的基本控制器。 现在我的问题是:我有一个基本的createactionresult+视图,在这个视图中,我希望用户能够从dropdownlist中选择一些值,这将简化新对象的创建。
我尝试用第二个表单来实现这一点在我看来第一个表单是基本的创建表单现在如果我想使用这些下拉列表,它们会互相过滤,所以首先用户必须指定一个大陆,然后,国家下拉列表只显示该大陆的国家,在他指定一个国家后,区域下拉列表将更新:基本视图的提交始终自动调用


因此,让下拉列表自己更新并不是问题:问题在于,当下拉列表更新时,create的表单会自动进行验证

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

//
// 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>
}
我已经找了至少一天了,我不知道怎么解决这个问题

关于
Alexander

或者1使用JQuery并加载下拉列表,其中包含控制器返回的部分视图,或者2您可以使用AJAX调用,将您的值作为从实体映射的JSON对象返回,您可以在下拉列表中呈现它们。这样,您的表单不会在每次更新下拉列表时提交

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

JQUERY

GetCountries视图


你的问题不清楚,但几乎清楚;我想如果你用更多的信息来扩展这句话:现在如果我想使用这些下拉列表,它们会互相过滤,基本视图的提交总是自动调用的。这样,每个人都会明白你的问题。因此,首先用户必须指定一个大陆,然后国家下拉列表仅显示该大陆的国家,在他指定一个国家后,区域下拉列表将更新:sry for the big post:sPerhaps此帖子将帮助您:。答案是使用AJAX获取新选项作为JSON响应,然后替换所选项目。让下拉列表更新本身并不是问题:而是当下拉列表更新时,create表单会自动进行验证updated@AlexanderVanLoock请在你的问题中加上这个。我不熟悉jQuery:s,所以我不知道如何实现这个解决方案jQuery是一个javascript库。这真的很容易。您需要做的是包括JQUERY库,并将我提供给视图的代码位添加到标记中。它所做的是将该函数与元素的onchange事件相关联,并在触发时执行。但如果更容易的话,也可以使用纯Javascript实现这一点。我可以将JQUery翻译成纯Javascript并编辑我的响应。如果这不太麻烦的话,你会真的帮助我:我想我在这方面有点超前:我还没有用纯Javascript开发AJAX调用,但我会尝试找出方法,除非其他人在这里帮助我们。但是我必须强调的是,这确实是很容易做到的,你不必为你的项目增加太多,相信我。如果你决定使用它,请毫不犹豫地问我你是否被什么东西卡住了;
<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>
@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);
    }
@model IEnumerable<SchoolCup.Domain.Country>

@Html.DropDownListFor( 0, Model)