MVC Razor将模型从View传递给Controller

时间:2017-01-20 13:48:31

标签: c# asp.net-mvc-4 razor

我的控制器调用视图:

    public ActionResult Excel()
    {


        myClass arg = new myClass(string arg1, object arg2);
        return View(arg);
    }

我的观点:

@model myClass
@using (Html.BeginForm("createXML", "Excel", new { arg = Model }))
{ {  @Html.ValidationSummary(true)
<div class="container-full" style="background-color:aliceblue">
    <h3 class="= container">Einstellungen:</h3>
    <div class="container">
        <div class="row">
            <div class="col-md-3">
                <div class="panel-group">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h4 class="panel-title">
                                <a data-toggle="collapse" href="#collapse1">nk</a>
                            </h4>
                        </div>
                        <div id="collapse1" class="panel-collapse collapse">
                            <ul class="list-group">
                                <li class="list-group-item">
                                    <form action="select.html">
                                        <label>
                                            Anzahl:
                                            <select name="decimal">
                                                <option>4</option>
                                                <option>5</option>
                                                <option>6</option>
                                                <option>7</option>
                                                <option>8</option>
                                                <option>9</option>
                                                <option>10</option>
                                            </select>
                                        </label>
                                    </form>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="panel-group">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h4 class="panel-title">
                                <a data-toggle="collapse" href="#collapse2">sign</a>
                            </h4>
                        </div>
                        <div id="collapse2" class="panel-collapse collapse">
                            <ul class="list-group">
                                <li class="list-group-item">
                                    <form action="select.html">
                                        <label>
                                            left:
                                            <select name="decimal">
                                                <option>4</option>
                                                <option>5</option>
                                                <option>6</option>
                                                <option>7</option>
                                                <option>8</option>
                                                <option>9</option>
                                                <option>10</option>
                                            </select>
                                        </label>
                                    </form>
                                </li>
                                <li class="list-group-item">
                                    <form action="select.html">
                                        <label>
                                            right:
                                            <select name="decimal">
                                                <option>4</option>
                                                <option>5</option>
                                                <option>6</option>
                                                <option>7</option>
                                                <option>8</option>
                                                <option>9</option>
                                                <option>10</option>
                                            </select>
                                        </label>
                                    </form>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="panel-group">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h4 class="panel-title">
                                <a data-toggle="collapse" href="#collapse3">Optionen</a>
                            </h4>
                        </div>
                        <div id="collapse3" class="panel-collapse collapse">
                            <form>
                                <fieldset>
                                    <ul class="list-group">
                                        <li class="list-group-item">
                                            <label>
                                                @Html.CheckBoxFor(m => m.P5_VerkBesch)
                                                @*<input type="checkbox" name="verkBesch" value="1">*@
                                                Verk
                                            </label>
                                        </li>
                                        <li class="list-group-item">
                                            <label>
                                                @Html.CheckBoxFor(m => m.P5_SpezBesch)
                                                @*<input type="checkbox" name="sFonds" value="1">*@
                                                spezbesch
                                            </label>
                                        </li>
                                        <li class="list-group-item">
                                            <label>

                                                <input type="checkbox" name="kgpr" value="1">
                                                kgpr
                                            </label>
                                        </li>
                                        <li class="list-group-item">
                                            <label>
                                                @Html.CheckBoxFor(m => m.P5_AD)
                                                @*<input type="checkbox" name="Ad" value="1">*@
                                                ad
                                            </label>
                                        </li>
                                        <li class="list-group-item">
                                            <label>

                                                <input type="checkbox" name="md" value="1">
                                                md
                                            </label>
                                        </li>
                                        <li class="list-group-item">
                                            <label>
                                                <input type="checkbox" name="beschText" value="1">
                                                beschtxt
                                            </label>
                                        </li>
                                        <li class="list-group-item">
                                            <label>
                                                @Html.CheckBoxFor(m => m.P5_Uni)
                                                @*<input type="checkbox" name="uni" value="1">*@
                                                uni
                                            </label>
                                        </li>
                                    </ul>
                                </fieldset>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="panel-group">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h4 class="panel-title">
                                <a data-toggle="collapse" href="#collapse4">Weitere Einstellungen</a>
                            </h4>
                        </div>
                        <div id="collapse4" class="panel-collapse collapse">
                            <ul class="list-group">
                                <li class="list-group-item">
                                    <div>

                                        <label>Anzahl Dateien pro:</label>
                                        <br />
                                        @*<small>123</small>*@
                                        @Html.TextBoxFor(i => i.P5_ANZAHL, new { @type = "number", @style = "max-width: 100%" })

                                    </div>
                                </li>
                                <li class="list-group-item">
                                    <div>
                                        <label>Datum ändern:</label>
                                        <br />
                                        <p>
                                            @Html.TextBoxFor(m => m.P5_DATUM, new { @id = "datepicker", @type = "text", @style = "width: 100%" })
                                            @*<input type="text" id="datepicker" style="width:100%">*@
                                        </p>
                                    </div>
                                </li>
                                <li class="list-group-item">
                                    <div>
                                        <label>Fds:</label>
                                        <br />
                                        @Html.TextAreaFor(m => m.P5_Fundstelle, new { @rows = "4", style = "width: 100%" })
                                        @*<textarea style="max-width:100%;" rows="4"></textarea>*@
                                    </div>
                                </li>
                                <li class="list-group-item">
                                    <div>
                                        <label>Anhaltspunkte :</label>
                                        <br />
                                        @*<small>123</small>*@
                                        @Html.TextBoxFor(m => m.P5_Anhaltspunkte, new { style = "width: 100%" })
                                    </div>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="container">
        <h2>Excel</h2>
        <div>
            <form id="myForm" method="post"
                  enctype="multipart/form-data"
                  action="">
                <div>
                    <h1>File Upload</h1>
                    @if (!IsPost)
                    {
                        @FileUpload.GetHtml(
        initialNumberOfFiles: 2,
        allowMoreFilesToBeAdded: true,
        includeFormTag: true,
        addText: "Add another file",
        uploadText: "Upload")
                    }
                    <span>@message</span>
                </div>

            </form>

            <input type="submit" value="Submit" />
        </div>
    </div>
</div>

}

应该调用的控制器函数:

  public ActionResult createXML(myClass arg)
    {

//Code here...
        return View("~/Views/Excel/Excel.cshtml");

    }

我的问题是&#34; arg&#34;作为null传递给createXML。我无法弄清楚原因。我不得不说我通常是WPF / WinForms程序员。

更新

我稍微更新了我的代码,以使其更清晰。

更新2

我发现了问题的一部分。我的对象只有一个构造函数,我需要一些paramateres。这会以某种方式产生错误。我创建了一个没有任何参数的构造函数,它可以工作,但是当我启动表单并且不使用传递给视图的对象时,它以某种方式创建了一个新对象。

2 个答案:

答案 0 :(得分:3)

像这样更改视图

datum

将上面的内容添加到createxml

@using (Html.BeginForm("createXML", "Excel", FormMethod.Post))

<强>更新

我尝试了你的代码并且工作了。这是代码

HomeController代码。您可以将此方法放在ExcelController中

[HttpPost]

Home / Excel.cshtml代码

public ActionResult Excel()
        {


            myClass arg = new myClass();
            arg.p = 5;
           return View(arg);
        }

        [HttpPost]
        public ActionResult createXML(myClass arg)
        {

            int a = arg.p;
            return View();

        }

答案 1 :(得分:1)

您使用了错误的重载。

您可以使用此重载。 BeginForm( this HtmlHelper htmlHelper, string actionName, string controllerName, Object routeValues, FormMethod method, Object htmlAttributes

@Html.BeginForm("createXML", "Excel", new { arg = Model },FormMethod.Post,null)

或删除参数并使用表单中的隐藏字段:

@using(Html.BeginForm("createXML", "Excel",FormMethod.Post))
{
    .....
  @Html.Hidden("arg", Model)
}