2013-05-02 44 views
2

因此,我構建了一個簡單的mvc4應用程序,我創建了所有基礎模型以創建數據庫,從這些類中我可以自然地創建具有匹配視圖的基本控制器。

現在我的問題:我有基本的創建actionresult +視圖,並在這個視圖中,我希望用戶能夠從下拉列表中選擇一些值,這將使創建新對象更簡單。

如果我想使用這些下拉菜單(它們相互過濾(所以首先用戶必須指定一個大洲,那麼第一個是基本創建表單),我嘗試在第二個表單中實現這一點這些國家的下拉菜單僅顯示來自該大陸的國家,並且在他指定了國家後,區域下拉菜單得到更新:)))總是自動調用基本視圖的提交。在一個視圖中有兩種形式的Mvc4

這樣做的下拉菜單更新自己不是問題:■它是在下拉菜單更新

這是控制器在下拉列表過濾對方

// 
// 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> 
} 

我一直在尋找,每天至少,我不知道如何解決這個

與問候 亞歷山大

+0

你的問題還不清楚,但它幾乎是明確的。 ;)我認爲如果你用更多的信息擴展這個句子:'現在如果我想使用這些下拉列表(它們相互過濾),基本視圖的提交總是自動調用。「那麼你的問題就會變得清晰。 – 2013-05-02 21:14:06

+0

首先用戶必須指定一個大陸,然後這些國家的下拉菜單僅顯示來自該大陸的國家,並且在他指定了一個國家後,該區域下拉菜單得到更新:) sry for the big post:s – 2013-05-02 21:30:36

+0

也許這篇文章可以幫助您: http://stackoverflow.com/questions/5497524/easiest-way-to-create-a-cascade-dropdown-in-asp-net-mvc-3-with-c-sharp。答案使用AJAX作爲JSON響應獲取新選項,然後替換選擇的項目。 – Jasen 2013-05-02 21:55:44

回答

2

怎麼樣:(1)使用jQuery和裝載掇(2)您可以進行AJAX調用,將您的值作爲從您的實體映射的JSON對象返回,並且您可以在下拉列表中呈現它們。這樣,每次更新下拉列表時,您的表單都不會被提交。

第一種解決方案可能是這個樣子:

JQUERY

<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> 

(EDIT)

爲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> 

免責聲明:這是我從來沒有嘗試過,所以如果它是錯的不要猶豫,讓我知道,如果有必要

(END EDIT)糾正


控制器

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 VIEW

@model IEnumerable<SchoolCup.Domain.Country> 

@Html.DropDownListFor(0, Model) 
+0

我不熟悉jQuery:所以我不知道如何實現這個解決方案 – 2013-05-02 22:47:52

+0

Jquery是一個JavaScript庫。這很容易。你需要做的是包含JQUERY庫,並在