繁体   English   中英

我无法在控制器中发送模型

[英]I can't send my model in my controller

我将模型发送到控制器时遇到问题。 我使用带有ajax的按钮来更改页面,但是我需要第一页到第二页的模型。 我想在控制器中发送模型,但无法正常工作。

当我进入页面CreateAll时,renderpartial Follow可以显示Step1,但是如果我单击step2,我希望将mainModel发送到我的控制器,请使用带有子模型的局部视图。

我的模型是:

public class CreateAllStep
{
    public CreateStep1 Step1 { get; set; }
    public CreateStep2 Step2 { get; set; }
    public CreateAllStep(CreateStep1 step1, CreateStep2 step2)
    {
        this.Step1 = step1;
        this.Step2 = step2;
    }
}

我的控制器是(页面开始时):

public ActionResult Create()
{
    CreateStep1 step1=FillStep1();
    CreateStep2 step2 = FillStep2();
    CreateAllStep allStep = new CreateAllStep(step1, step2);
    return View(allStep);
}

我的控制器是(当我单击按钮时,它是我想发送模型的位置):

    [HttpPost]
    public ActionResult Create(String btn, CreateAllStep form)
    {
        if (Request.IsAjaxRequest())
        {
            if (btn != null)
            {
                if (btn == "Step1")
                {
                    return PartialView("Step1",form.Step1);//not work
                }
                else if (btn == "Step2")
                {
                    return PartialView("Step2");//work
                }
                else if(btn =="AllStep")
                {
                    return PartialView("AllStep");
                }
            }
        }
        return View();
    }

我的主要观点是:

    @model SiteWebEmpty.Models.CreateAllStep
@{
    ViewBag.Title = "Title";
}
<script type="text/javascript">
    $(function () {
        $('form').submit(function () {
            $.post(this.action, $(this).serialize(), function (data) {
                alert(data);
            });
            return false;
        });
    });
</script>

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

<h2>Title</h2>
@using (Ajax.BeginForm("Create", //controller action name
"CreateStep", //controller name
new AjaxOptions //ajax options that tell mvc how to perform the replacement
{
    UpdateTargetId = "ViewPage", //id of div to update
    HttpMethod = "Post" //how to call the controller action    
}, new { id = "FormName" }))
{
    @Html.ValidationSummary(true)
    <fieldset>
    <legend>Create </legend>
    <button type="submit" name="btn" value="Step1" id="Step1">Step 1</button>
    <button type="submit" name="btn" value="Step2" id="Step2">Step 2</button>
    <button type="submit" name="btn" value="AllStep" id="AllStep">All Step</button>

    <div id="ViewPage">
    @Html.Partial("Step1", Model)
    </div>        
    </fieldset>    
}

我的部分观点是:

@model SiteWebEmpty.Models.ArticleRequest.CreateArticle.ArticleRequestDisplayCreateAllStep

<fieldset>
<legend>Step 1</legend>
                @Html.LabelFor(step1 => step1.Step1.Customer)
                @Html.EditorFor(step1 => step1.Step1.Customer)
                @Html.ValidationMessageFor(step1 => step1.Step1.Customer)

                @Html.LabelFor(articleType => articleType.Step1.ArticleType)
                @Html.DropDownList("ArticleType", Model.Step1.ArticleType)
                @Html.ValidationMessageFor(articleType => articleType.Step1.ArticleType)

                @Html.LabelFor(model => model.Step1.LabelType)
                @Html.DropDownList("LabelType", Model.Step1.LabelType)
                @Html.ValidationMessageFor(model => model.Step1.LabelType)
</fieldset>

渲染html:

<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
<h2>Title</h2>
<form action="/CreateStep/Create?Length=13" data-ajax="true" data-ajax-method="Post" data-ajax-mode="replace" data-ajax-update="#ViewPage" id="FormName" method="post">    <fieldset>
    <legend>Create </legend>
    <button type="submit" name="btn" value="Step1" id="Step1">Step 1</button>
    <button type="submit" name="btn" value="Step2" id="Step2">Step 2</button>
    <button type="submit" name="btn" value="AllStep" id="AllStep">All Step</button>

    <div id="ViewPage">

<fieldset>
<legend>Step 1</legend>
                <label for="Customer">Customer</label>
                <input class="text-box single-line" data-val="true" data-val-required="Customer is required" id="Customer" name="Customer" type="text" value="" />
                <span class="field-validation-valid" data-valmsg-for="Customer" data-valmsg-replace="true"></span>

                <label for="ArticleType">Article Type</label>
                <select data-val="true" data-val-required="ArticleType is required" id="ArticleType" name="ArticleType"><option value="127">AR1 : New Product</option>
<option value="161">AR2 : Product Modification</option>
</select>
                <span class="field-validation-valid" data-valmsg-for="ArticleType" data-valmsg-replace="true"></span>

                <label for="LabelType">Label Type</label>
                <select data-val="true" data-val-required="LabelType is required" id="LabelType" name="LabelType"><option value="129">Private Label</option>
</select>
                <span class="field-validation-valid" data-valmsg-for="LabelType" data-valmsg-replace="true"></span>
</fieldset>

    </div>        
    </fieldset>    
</form>

谢谢你的帮助 :) !

您可以发布最终的HTML吗?

我认为您的@ Html.Partial(“ Step1”,Model.Step1)将呈现ID为Customer或ArticleType的输入文本,而不是Step1.Customer和Step1.ArticleType。 绑定到不存在的CreateAllStep.Customer的对象。

如果您有浏览器发送的标题,也会有所帮助。

更新:更改您的部分Step1,以接受CreateAllStep模型,然后重试

尝试在ViewModel上删除构造函数:

public CreateAllStep(CreateStep1 step1, CreateStep2 step2)
{
    this.Step1 = step1;
    this.Step2 = step2;
}

并将控制器代码更改为:

public ActionResult Create()
{
    CreateAllStep allStep = new CreateAllStep{Step1 = FillStep1(), Step2 = FillStep2()};
    return View(allStep);
}

数据绑定时,我遇到了带有参数的构造函数的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM