Asp.net mvc 4 如何将数据从windows弹出窗口传递到控制器

Asp.net mvc 4 如何将数据从windows弹出窗口传递到控制器,asp.net-mvc-4,razor,Asp.net Mvc 4,Razor,我创建了一个名为AddCust.cshtml的弹出窗口。一旦用户单击了btnSaveConsumer,它应该会选择输入的所有值。但我不知道如何通过控制器。我尝试像下面那样使用javascript,但它没有从AddCust.cshtml传递任何内容。请告知,谢谢 AddCust.cshtml @model HHIMS_Web_App.Models.ConsumerModel @{ } <br /> <div> <fieldset id="AddNewConsum

我创建了一个名为AddCust.cshtml的弹出窗口。一旦用户单击了btnSaveConsumer,它应该会选择输入的所有值。但我不知道如何通过控制器。我尝试像下面那样使用javascript,但它没有从AddCust.cshtml传递任何内容。请告知,谢谢

AddCust.cshtml

@model HHIMS_Web_App.Models.ConsumerModel
@{

}

<br />
<div>

<fieldset id="AddNewConsumer">


                      <br /> <br />
        <div>
            <div class="addConsumerInfo">
                @Html.LabelFor(model => model.HRN)
                @Html.EditorFor(model => model.HRN)
            </div>

            <div class="smallBox">
                @Html.LabelFor(model => model.DOB)
                @Html.EditorFor(model => model.DOB)

            </div>

        </div>

        <div>
            <div class="addConsumerInfo">
                @Html.LabelFor(model => model.GivenName1)
                @Html.EditorFor(model => model.GivenName1)

            </div>

        </div>

        <div>
            <div class="addConsumerInfo">
                @Html.LabelFor(model => model.FamilyName1)
                @Html.EditorFor(model => model.FamilyName1)
            </div>
            <div class="smallBox">
                @Html.LabelFor(model => model.Ethnicity)
                @Html.EditorFor(model => model.Ethnicity)

            </div>
        </div>

        <div>
            <div class="addConsumerInfo">
                @Html.LabelFor(model => model.GivenName2)
                @Html.EditorFor(model => model.GivenName2)

            </div>

        </div>

        <div>
            <div class="addConsumerInfo">
                @Html.LabelFor(model => model.FamilyName2)
                @Html.EditorFor(model => model.FamilyName2)
            </div>

        </div>

        <div>
            <div class="addConsumerInfo">
                @Html.LabelFor(model => model.Address)
                @Html.EditorFor(model => model.Address)

            </div>

            <div class="smallBox">
                @Html.LabelFor(model => model.CarerContactName)
                @Html.EditorFor(model => model.CarerContactName)

            </div>

        </div>



        <div>
            <div class="addConsumerInfo">
                @Html.LabelFor(model => model.Community)
                @Html.EditorFor(model => model.Community)

            </div>
            <div class="smallBox">
                @Html.LabelFor(model => model.CarerContact)
                @Html.EditorFor(model => model.CarerContact)

            </div>


        </div>

        <div>
            <div class="addConsumerInfo">
                @Html.LabelFor(model => model.State)
                @Html.EditorFor(model => model.State)

            </div>


        </div>

        <div>
             <div class="addConsumerInfo">
                @Html.LabelFor(model => model.PostCode)
                @Html.EditorFor(model => model.PostCode)
             </div>

        </div>

        <div>
             <div class="addConsumerInfo">
                @Html.LabelFor(model => model.Phone)
                @Html.EditorFor(model => model.Phone)
             </div>
             <div class="smallAddAndCancel">
                <input type="button" id="btnCancel" style="height:33px; width:70px; font-size:14px; background-color:#3399FF" class="k-button" title="Cancel" value="Cancel" onclick="window.close()" />
                <input type="button" id="btnSaveConsumer" style="height:33px; width:70px; font-size:14px; background-color:#3399FF" class="k-button" title="Save" value="Save" />

            </div>
        </div>

    </fieldset>

</div>


<script type="text/javascript">



    $(document).ready(function () {


        $('#btnSaveConsumer').click(function () {

            var hrn;
            var community;
            var ethnicity;
            var familyName;
            var givenName;

            if (hrn) {
                $filter.push({ field: "HRN", operator: "contains", value: hrn });
            }

            if (community) {
                $filter.push({ field: "Community", operator: "contains", value: community });
            }

            if (familyName) {
                $filter.push({ field: "FamilyName", operator: "contains", value: familyName });
            }

            if (givenName) {
                $filter.push({ field: "GivenName", operator: "contains", value: givenName });
            }

            if (ethnicity) {
                $filter.push({ field: "Ethnicity", operator: "contains", value: ethnicity });
            }

            $.ajax({
                type: 'POST',
                url: "@(Url.Content("~/ConsumerDetails/CreateConsumerList/"))",
                data: {
                        "HRN": hrn,
                        "Community":community,
                        "FamilyName1":familyName,
                        "GivenName1":givenName,
                        "Ethnicity":ethnicity,

                },

            });
        });


    });

</script>
@model HHIMS\u Web\u App.Models.ConsumerModel
@{
}



@LabelFor(model=>model.HRN) @EditorFor(model=>model.HRN) @LabelFor(model=>model.DOB) @EditorFor(model=>model.DOB) @LabelFor(model=>model.GivenName1) @EditorFor(model=>model.GivenName1) @LabelFor(model=>model.FamilyName1) @Html.EditorFor(model=>model.FamilyName1) @LabelFor(model=>model.Html) @EditorFor(model=>model.com) @LabelFor(model=>model.GivenName2) @EditorFor(model=>model.GivenName2) @Html.LabelFor(model=>model.FamilyName2) @Html.EditorFor(model=>model.FamilyName2) @LabelFor(model=>model.Address) @EditorFor(model=>model.Address) @LabelFor(model=>model.CarerContactName) @EditorFor(model=>model.CarerContactName) @LabelFor(model=>model.Community) @EditorFor(model=>model.Community) @LabelFor(model=>model.CarerContact) @EditorFor(model=>model.CarerContact) @LabelFor(model=>model.State) @EditorFor(model=>model.State) @LabelFor(model=>model.PostCode) @EditorFor(model=>model.PostCode) @LabelFor(model=>model.Phone) @EditorFor(model=>model.Phone) $(文档).ready(函数(){ $('#btnSaveConsumer')。单击(函数(){ var-hrn; var群落; 种族差异; var familyName; var Givename; 如果(hrn){ $filter.push({字段:“HRN”,运算符:“contains”,值:HRN}); } 国际单项体育联合会(社区){ $filter.push({field:“Community”,operator:“contains”,value:Community}); } if(家庭名称){ $filter.push({字段:“FamilyName”,运算符:“contains”,值:FamilyName}); } 如果(givenName){ $filter.push({字段:“GivenName”,运算符:“contains”,值:GivenName}); } if(种族){ $filter.push({字段:“种族”,运算符:“包含”,值:种族}); } $.ajax({ 键入:“POST”, url:@(url.Content(“~/ConsumerDetails/CreateConsumerList/”), 数据:{ “HRN”:HRN, “社区”:社区, “familyName 1”:familyName, “GivenName1”:givenName, “种族”:种族, }, }); }); });
控制器:

[HttpPost]
        public ActionResult CreateConsumerList(ConsumerModel model)
        {
            if (ModelState.IsValid)
            {

                HHIMS_DataAccessLayer.Consumers dalModel = new HH.Consumers();
                Mapper.CreateMap<ConsumerModel, HH.Consumers>();
                Mapper.Map(model, dalModel);

                dbConsumer.SaveConsumer(dalModel);

            }
            return RedirectToAction("Index");

        }
[HttpPost]
公共操作结果CreateConsumerList(ConsumerModel模型)
{
if(ModelState.IsValid)
{
HHIMS_DataAccessLayer.Consumers dalModel=新的HH.Consumers();
CreateMap();
Map(model,dalModel);
dbConsumer.SaveConsumer(dalModel);
}
返回操作(“索引”);
}

将所有字段封装在一个表单中,使其更加简单:

@using (Html.BeginForm("action","contoroller")) {
    @Html.EditorFor(model => model.HRN)
}
然后提交你的表格

var $form = $("#the_id_of_the_form");
// want to validate your form?
// $form.validate();
// if (!$form.valid()) return;
$.ajax({
    type: 'POST',
    url: "@Url.Action("CreateConsumerList","ConsumerDetails")",
    data: $form.serialize(),            
});
或者,如果您不想出于任何原因使用表单:

$.ajax({
        type: 'POST',
        url: "@Url.Action("CreateConsumerList","ConsumerDetails")",
        data: {
            HRN: $("#HRN").val()
            // do the same for the rest of the fields
        }
    });

您不需要JavaScript来完成此操作。通过在中收集表单字段值,可以添加更多工作。单击事件并调用
$.ajax
函数。我觉得这是额外的一步。我建议使用
AjaxHelper

@model HHIMS_Web_App.Models.ConsumerModel

    @using(Ajax.BeginForm("CreateConsumerList", "ControllerName",new AjaxOptions {Success:, HttpMethod = "Post" }))
    {
    <div>
       <fieldset id="AddNewConsumer">
// All your form fields here
<div class="smallAddAndCancel">
                <input type="button" id="btnCancel" style="height:33px; width:70px; font-size:14px; background-color:#3399FF" class="k-button" title="Cancel" value="Cancel" onclick="window.close()" />
                <input type="submit" id="btnSaveConsumer" style="height:33px; width:70px; font-size:14px; background-color:#3399FF" class="k-button" title="Save" value="Save" />

            </div>
     </fieldset>

    </div>
    }
@model HHIMS\u Web\u App.Models.ConsumerModel
@使用(Ajax.BeginForm(“CreateConsumerList”、“ControllerName”、新的AjaxOptions{Success:,HttpMethod=“Post”}))
{
//所有表单字段都在这里
}

“您不需要JavaScript”,但您正在使用ajax;)我指的是$0.ajax呼叫。这是额外的一步。他为什么不在System.Web.MVC.ajax中充分利用MVC framkework谢谢你编辑了你的答案,这是一个更好的内容。关于“所有不必要的”,这叫做“偏好”。使用
AjaxHelper
并不是最好的方法,尤其是在一个大型项目中,用户界面人员都是优秀的jquery开发人员和CSS专家。要求是您必须拥有没有服务器端代码的UI。您是否遇到过这样一个项目:它会告诉您使用表单标记而不是使用
使用
?我知道是的。所以底线是,这是一种偏好,“不必要”并不是最好的术语;)@沃恩。谢谢你的建议。我将用一些较轻的词来代替这个词。