Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 在新窗口中打开MVC视图,但将模型传递给动作,而不是单个参数_Jquery_Asp.net Mvc - Fatal编程技术网

Jquery 在新窗口中打开MVC视图,但将模型传递给动作,而不是单个参数

Jquery 在新窗口中打开MVC视图,但将模型传递给动作,而不是单个参数,jquery,asp.net-mvc,Jquery,Asp.net Mvc,我喜欢通过将参数作为模型传递来调用MVC操作,并希望在新窗口中显示返回视图 我从Javascript中尝试了以下两种不同的方法,但都不起作用: 1. window.open( '@Url.Action("EIDLookUp", "LookUp")?details=' + JSON.stringify(labInputLDAPInput), '', width=1024, height=665, titlebar=1, toolbar=No

我喜欢通过将参数作为模型传递来调用MVC操作,并希望在新窗口中显示返回视图

我从Javascript中尝试了以下两种不同的方法,但都不起作用:

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);
    }
})
我的控制器操作如下所示:

[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);
}
[HttpPost]
public ActionResult EIDLookUp(查找详细信息)
{
GetDataFromLDAP empData=新建GetDataFromLDAP();
IEnumerable employeeList=
empData.geteidlookUpdatea(details.eidlook,details.activeOnly);
//为视图模型指定值
//LookUpDetails=新的LookUpDetails();
details.employeeList=employeeList;
//details.found=found;
//details.fieldID=fieldID;
//details.multiple=多个;
//details.activeOnly=activeOnly;
details.row=1;
返回视图(“EIDLookUp”,详细信息);
}

请让我知道如何使这项工作。提前感谢。

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

例如,我起诉knockout就是为了这么做:

$.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
对象与敲除数据绑定进行映射来填充视图中的对象。()

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

$.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();
   }
});
并使用jquery
.val()
ex:
$('name').val()将
LookUpDetails
与表单数据手动映射


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

请参阅我的评论,确认正在工作,并将以下内容用作测试。 还没有播放返回的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>
}
@model TestModel
@使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post,new{@name=“testForm”}))
{
@EditorFor(m=>m.FormId)
@EditorFor(m=>m.UserValue)
}
@节脚本{
$(“.submit dummy”)。在(“单击”上,函数(){
$.ajax({
url:'@url.Action(“索引”,“主页”),
数据:$(“表单[name=testForm]”)。序列化(),
键入:“POST”,
数据类型:“html”,
成功:功能(响应){
var w=window.open();
$(w.document.body).html(response.responseText);
}
});
});
}

mvandersteen的回答完全正确。而且是罕见的。当我在dropdownlistfor中更改所选值并执行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>
}
@节脚本
{
@style.Render(“~/Content/css”)//这些脚本对于本例并不重要
@Scripts.Render(“~/bundles/modernizer”)
@Scripts.Render(“~/bundles/jquery”)
@Scripts.Render(“~/bundles/jqueryui”)
@Scripts.Render(“~/Scripts/SGLRegistration.js”)
//这里很有趣
$(.jsddlCustomer”).on(“更改”,函数(){
$.ajax({
url:'@url.Action(“索引”,“主页”),
数据:$(“表单[name=dataRegistrationForm]”)。序列化(),
键入:“POST”,
数据类型:“html”,
成功:功能(响应){
var w=window.open();
$(w.document.body).html(response.responseText);
}
});
});
}
我的dropdownlist是这样声明的:

<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>

@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”})

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

我认为您可以通过上面使用的opt.2从表单发回数据。因此,对于数据参数,请使用序列化表单,而不是您使用的var。数据:$('').serialize(),请尝试一下。非常感谢。您的示例是非常罕见的视图更改示例之一。我修改了您的示例以用于DropDownList for。我所要做的就是改变。在(“点击”),到。在(“改变”。下面我将发布我的改变作为例子。Bedankt!