Asp.net mvc ASP.net MVC中未刷新的控件
我正在学习微软ASP MVC框架。这是我这一页的奇怪之处 我在页面上有两个下拉列表。第一个将表单发回,并将参数传递给控制器。控制器更新第二个下拉列表和其他控件,如两个文本框。我会将数据传递给viewdata。并将它们分配给控件(SelectList到DropDownList,string到TextBox)。但这些控制仍然与邮报之前一样。我怎样才能解决这个问题?提前谢谢 问候 编辑: 谢谢!这是我的密码: 视图:Asp.net mvc ASP.net MVC中未刷新的控件,asp.net-mvc,Asp.net Mvc,我正在学习微软ASP MVC框架。这是我这一页的奇怪之处 我在页面上有两个下拉列表。第一个将表单发回,并将参数传递给控制器。控制器更新第二个下拉列表和其他控件,如两个文本框。我会将数据传递给viewdata。并将它们分配给控件(SelectList到DropDownList,string到TextBox)。但这些控制仍然与邮报之前一样。我怎样才能解决这个问题?提前谢谢 问候 编辑: 谢谢!这是我的密码: 视图: 编辑:在第二个控制器中调用ModelState.Clear()修复该问题。感谢所有提
编辑:在第二个控制器中调用ModelState.Clear()修复该问题。感谢所有提供建议的人!真的谢谢 使用强类型
IEnumerable
-我觉得它比SelectList
好。尝试此操作(您是否检查了调试器中的int country
参数?)
希望这有帮助您是否在SelectList中设置selectedValue(下面代码段中构造函数的第二个参数)
var items=newselectlist(新列表{“一”、“二”、“三”},“二”);
似乎您提供的信息太少了。请发布一些代码,这将帮助我们从视图中发布代码,以及获取和发布操作。我已经阅读了您之前的问题()-您是否使用AJAX?哇,您正在复制ASP.NET Webforms可怕的回发功能?为什么不直接使用网络表单呢?谢谢!我要试试强类型的IEnumerable。作为id部分,我一定要使用正确的id。我还可以将id和信息打印到页面上。只是没法定下定力。代码:选择列表=(选择列表)视图数据[“状态”];打印出来的身份证就是我想要的。
<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();
}
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();
}
var items = new SelectList(new List<string> {"one", "two", "three"}, "two");