我想使用Ajax.Actionlink将模型数据从一个View传输到另一个View(在对话框中)我在Post Action上获取空值
这是我的观点
@using (Ajax.BeginForm("", new AjaxOptions { }))
{
for (int i = 0; i < Model.city.Count; i++)
{
<div class="editor">
<p>
@Html.CheckBoxFor(model => model.city[i].IsSelected, new { id = "check-box-1" })
@Html.HiddenFor(model => model.city[i].Id)
@Ajax.ActionLink(Model.city[i].Name, "PopUp", "Home",
new
{
@class = "openDialog",
data_dialog_id = "emailDialog",
data_dialog_title = "Cities List",
},
new AjaxOptions
{
HttpMethod = "POST"
})
@Html.HiddenFor(model => model.city[i].Name)
</p>
</div>
}
}
使用Ajax.Actionlink我正在使用ajax脚本创建一个对话框
此视图的控制器类是
public ActionResult Data()
{
var cities = new City[] {
new City { Id = 1, Name = "Mexico" ,IsSelected=true},
new City { Id = 2, Name = "NewJersey",IsSelected=true },
new City { Id = 3, Name = "Washington" },
new City { Id = 4, Name = "IIlinois" },
new City { Id = 5, Name = "Iton" ,IsSelected=true}
};
var model = new Citylist { city = cities.ToList() };
//this.Session["citylist"] = model;
return PartialView(model);
}
另一个用于显示帖子操作数据的视图是
@model AjaxFormApp.Models.Citylist
@{
ViewBag.Title = "PopUp";
}
<h2>
PopUp</h2>
<script type="text/javascript">
$(function () {
$('form').submit(function () {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
var checkedAtLeastOne = false;
$('input[id="check-box-2"]').each(function () {
if ($(this).is(":checked")) {
checkedAtLeastOne = true;
// alert(checkedAtLeastOne);
}
});
if (checkedAtLeastOne == true) {
// alert("Test");
$('#div1').show();
$(".dialog").dialog("close");
}
else {
// alert("NotSelected");
$('#div1').hide();
$('#popUp').html(result);
$('#popUp').dialog({
open: function () { $(".ui-dialog-titlebar-close").hide(); },
buttons: {
"OK": function () {
$(this).dialog("close");
}
}
});
}
}
});
return false;
});
});
</script>
<div style="display: none" id="div1">
<h4>
Your selected item is:
</h4>
</div>
@using (Ajax.BeginForm(new AjaxOptions { }))
{
for (int i = 0; i < Model.city.Count; i++)
{
<div class="editor">
<p>
@Html.CheckBoxFor(model => model.city[i].IsSelected,new { id = "check-box-2" })
@Html.HiddenFor(model => model.city[i].Id)
@Html.LabelFor(model => model.city[i].Name, Model.city[i].Name)
@Html.HiddenFor(model => model.city[i].Name)
</p>
</div>
}
<input type="submit" value="OK" id="opener" />
}
@*PopUp for Alert if atleast one checkbox is not checked*@
<div id="popUp">
</div>
我的帖子控制器动作结果类是
[HttpPost]
public ActionResult PopUp(Citylist model)
{
if (Request.IsAjaxRequest())
{
//var model= this.Session["citylist"];
return PartialView("PopUp",model);
}
return View();
}
我的模型类是
public class City
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; }
}
public class Citylist
{
public IList<City> city { get; set; }
}
答案 0 :(得分:0)
您正在从actionlink调用Popup操作。相反,你应该把提交按钮放在for循环之外。并为您的表单提供正确的操作参数