1

I like to call an MVC action by passing parameters as a Model and I want to show the return view in a new Window.

I tried in two different ways as below from Javascript but it is not working:

1.

window.open(
    '@Url.Action("EIDLookUp", "LookUp")?details=' + JSON.stringify(labInputLDAPInput), 
    '',
    width=1024,    
    height=665,
    titlebar=1,
    toolbar=No,
    menubar=No,
    scrollbars=Yes,
    resizable=Yes,
    location=No,
    directories=No,
    status=Yes'
);

2.

$.ajax({
    url: '@Url.Action("EIDLookUp", "LookUp")',
    data: labInputLDAPInput,
    type: 'POST',
    dataType: 'html',
    success: function (response) {
        var w = window.open();
        $(w.document.body).html(response.responseText);
    }
})

My Controller action looks like this:

[HttpPost]
public ActionResult EIDLookUp(LookUpDetails details) 
{

    GetDataFromLDAP empData = new GetDataFromLDAP();
    IEnumerable<SearchClientResult> employeeList = 
        empData.GetEIDLookUpData(details.eidLookUp, details.activeOnly);

    // Assign values to View Model
    //LookUpDetails details = new LookUpDetails();
    details.employeeList = employeeList;
    //details.found = found;
    //details.fieldID = fieldID;
    //details.multiple = multiple;
    //details.activeOnly = activeOnly;
    details.row = 1;

    return View("EIDLookUp", details);
}

Please let me know how to make this work. Thanks in advance.

4

3 回答 3

2

参考我的评论,确认工作,使用以下作为测试。还没有使用返回的 html,但控制器中的 viewModel 将包含表单中的所有输入值。

视图模型

public class LookUpDetails
{
    public int? FormId { get; set;  }
    public string UserValue { get; set; }
}

控制器动作

    [HttpPost]
    public ActionResult EIDLookUp(TestModel viewModel)
    {
        if (ModelState.IsValid)
        {
            ViewBag.Message = "For Submitted with uservalue from form = " + viewModel.UserValue;
        }
        return View(viewModel);
    }

cshtml

@model TestModel
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @name = "testForm" }))
{
    @Html.EditorFor(m => m.FormId)
    @Html.EditorFor(m => m.UserValue)

    <input type="button" class="submit-dummy" value="Submit" />
}
@section scripts {
    <script type="text/javascript">
        $(".submit-dummy").on("click", function () {
            $.ajax({
                url: '@Url.Action("Index", "Home")',
                data: $("form[name=testForm]").serialize(),
                type: 'POST',
                dataType: 'html',
                success: function (response) {
                    var w = window.open();
                    $(w.document.body).html(response.responseText);
                }
            });
        });
    </script>
}
于 2013-09-16T13:45:38.613 回答
0

我能够将模型发送到 MVC 操作,但是发送的模型必须是 json 格式,并且它的所有属性名称都需要与实体匹配。

例如,我正在起诉淘汰赛来做到这一点:

$.ajax({
    type: "POST",
    url: "/yourcontroller/EIDLookUp",
    contentType: "application/json; charset=utf-8",
    data: ko.toJSON(LookUpDetails),
    dataType: "json",
    error: function (resp) {
         alert("ERROR\n" + resp.responseText);
    },
    success: function (data) {
         $(".notification").show();
    }
});
LookUpDetails = {
  Name : ko.observable()
};

您可以通过将对象与剔除数据绑定进行映射来填充LookUpDetails视图中的对象。( http://knockoutjs.com/ )

如果您不想使用淘汰赛或任何其他库,我认为您仍然可以使用:

$.ajax({
   type: "POST",
   url: "/yourcontroller/EIDLookUp",
   contentType: "application/json; charset=utf-8",
   data: JSON.stringify(LookUpDetails),
   dataType: "json",
   error: function (resp) {
         alert("ERROR\n" + resp.responseText);
   },
   success: function (data) {
         $(".notification").show();
   }
});

LookUpDetails并使用 jquery .val() ex手动映射表单数据:$('name').val()

这里的关键是 json 对象必须与您的实体匹配。

于 2013-09-16T13:41:05.333 回答
0

mvandersteen 的答案是完全正确的。和一个罕见的。当我更改下拉列表中的选定值并执行 httppost 并将模型作为参数时,我使用下面的代码。

@section Scripts
{
    @Styles.Render("~/Content/css")  // these scripts are not important for this example
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryui")
    @Scripts.Render("~/Scripts/SGLRegistration.js")

    <script type="text/javascript">  //here it gets interesting
        $(".jsddlCustomer").on("change", function () {
            $.ajax({
                url: '@Url.Action("Index", "Home")',
                data: $("form[name=dataRegistrationForm]").serialize(),
                type: 'POST',
                dataType: 'html',
                success: function (response) {
                    var w = window.open();
                    $(w.document.body).html(response.responseText);
                }
            });
        });
    </script>
}

我的下拉列表是这样声明的:

<div id="tempCustomerselect">
    @Html.LabelFor(m => m.CustomerName)
    @Html.DropDownListFor(m => m.PitchID, new SelectList((new SGLDataRegistration.Models.CustomerModel().GetRoles()).OrderBy(x => x.CustomerName), "PitchID", "CustomerName"), new {id = "ddlCustomer", @class="jsddlCustomer"})
</div>

希望这对某人有帮助,我为此搜索了很多小时。

于 2013-12-29T18:15:27.377 回答