0

我想使用 Ajax.Actionlink 将模型数据从一个视图传输到另一个视图(在对话框中),如果我在 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; }

}
4

1 回答 1

0

您正在从 actionlink 调用 Popup 操作。相反,您应该将提交按钮置于 for 循环之外。并为您的表单提供正确的操作参数

于 2012-08-16T06:25:28.680 回答