简体   繁体   中英

MVC Razor passing Model from View to Controller

My Controller which calls the View:

    public ActionResult Excel()
    {


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

My View:

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

}

Controller Function which should be called:

  public ActionResult createXML(myClass arg)
    {

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

    }

My Problem is that "arg" is passed as null to createXML. I can't figure out why. One has to say that I am normally a WPF/WinForms Programmer.

UPDATE

I updated my code a bit, to make it clearer maybe.

UPDATE 2

I found out a part of the problem. My Object has only one constructor, where i need some paramateres. This generates somehow an error. I created a constructor without any parameter and it works, but it somehow creates a new object when I start my form and does not use the object i passed on to the view.

change view like this

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

and

add this above createxml

[HttpPost]

UPDATE

I tried your codes and worked. Here is codes

HomeController codes. You can place this methods in your ExcelController

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();

        }

Home/Excel.cshtml codes

@model Identity.Controllers.myClass
@{
    Layout = null;
}

@using (Html.BeginForm("createXML", "Home", FormMethod.Post))
{  @Html.ValidationSummary(true)
    <div class="container-full" style="background-color:aliceblue">
        <h3 class="= container">Einstellungen:</h3>
        <div class="container">
            @Html.TextBoxFor(x => x.p)
            <input type="submit" value="Submit" />
        </div>
    </div>
}

You are using wrong overload.

You can use this overload. BeginForm( this HtmlHelper htmlHelper, string actionName, string controllerName, Object routeValues, FormMethod method, Object htmlAttributes )

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

or remove the argument and use hidden field within the form:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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