2009-07-27 73 views
-2

我在研究Microsoft ASP MVC框架。這是我的頁面奇怪的東西。在ASP.net中未刷新的控件MVC

我在頁面上有兩個下拉列表。第一個將表單發回,將參數傳遞給控制器​​。控制器更新第二個下拉列表和其他控件,如兩個文本框。我確實將數據傳遞給viewdata。並將它們分配給控件(SelectList爲DropDownList,並將字符串分配給TextBox)。但那些征服者仍然像以前一樣。我該如何解決這個問題?提前致謝!

問候

編輯:

謝謝!這裏是我的代碼:

查看:

<script type="text/javascript" language="javascript"> 
    function Postback() { 
     var control = document.getElementById("Country"); 
     var parameter = document.getElementById("CountryName"); 

     parameter.value = control.value; 

     document.forms.item(0).submit(); 

    } 

</script> 

<%Html.BeginForm();%> 

    <fieldset> 
     <legend>Fields</legend> 
     <p style="height: 0px"> 
     <input type="hidden" id="CountryName" name="CountryName" value="" /> 
     </p> 
     <p> 
      <label for="Country">Country:</label> 
      <%=Html.DropDownList("Country", (SelectList)ViewData["Countries"]), new { onchange="Postback()" })%> 
     </p> 
     <p> 
      <label for="State">State:</label> 
      <%=Html.DropDownList("State", (SelectList)ViewData["States"])%> 
      </p> 
     <p> 
      <label for="Brief Intro">Introduction:</label> 
      <%= Html.TextBox("Intro", ViewData["Introduction"]) %> 
     </p> 
     <p> 
      <input type="submit" value="Save" /> 
     </p> 
    </fieldset> 

<%Html.EndForm(); %> 

控制器:

public ActionResult Info() 
{ 
    ViewData["Countries"] = new SelectList(_db.Coutries.ToList(), "Id", "Name"); 
    return View(); 
} 

AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Info(int country) 
{ 
    ViewData["Countries"] = new SelectList(_db.Coutries.ToList(), "Id", "Name", country); 

    ViewData["States"] = new SelectList(_db.States.Where(s => s.countryid == country).ToList(), "Id", "Name"); 

    ViewData["Info"] = _db.CountryInfo.SingleOrDefault(info => info.countryid == country).Content; 

    return View(); 
} 

編輯:第二控制器中調用ModelState.Clear()解決這一問題。感謝所有提供建議的人!真的感謝!

+0

似乎你提供的信息太少了。 – 2009-07-27 05:48:44

+1

發佈一些代碼,這將對我們有很大幫助 – 2009-07-27 05:50:12

+0

請從View發佈代碼以及GET和POST操作。 – 2009-07-27 06:48:52

回答

1

使用強類型IEnumerable<SelectListItem> - 我相信它比SelectList好。試試這個(你檢查在調試器中int country參數):

AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Info(int country) 
{ 
    ViewData["Countries"] = _db.Countries.Select(x => new SelectListItem { 
     Text = x.Name, 
     Value = x.Id, // or countryId ? 
     Selected = x.Id == country 
    }).ToList(); 

    ViewData["States"] = _db.States.Where(s => s.countryid == country) 
     .Select(x => new SelectListItem { 
      Text = x.Name, 
      Value = x.Id 
     }).ToList(); 

    // Info? or Intro? (there is only Intro in View) 
    ViewData["Info"] = _db.CountryInfo.SingleOrDefault(info => info.countryid == country).Content; 

    return View(); 
} 

希望這有助於

0

你設置了selectedValue在選擇列表的(第二個參數去構造在下面的代碼段)?

var items = new SelectList(new List<string> {"one", "two", "three"}, "two");