我不能在我的控制器中发送我的模型

时间:2012-03-20 12:06:22

标签: c# asp.net-mvc asp.net-mvc-3 asp.net-ajax asp.net-mvc-4

将模型发送到控制器时遇到问题。 我使用带有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>

感谢您的帮助:)!

2 个答案:

答案 0 :(得分:1)

您可以发布最终的HTML吗?

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

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

更新:更改部分Step1,接受CreateAllStep模型并重试

答案 1 :(得分:0)

尝试删除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);
}

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