Maximus
Maximus

Reputation: 3448

Model is null with Html.BeginForm() ASP.NET MVC

I encountered following problem:
user visits site, clicks "Add" and then it's sending back to Controller, Model is retrieved and send to View one more time. Inside view, I check whether Model is not null and displays data.

@if (Model != null)
    {
        <div id="appInfo">
            <table>
                <tr>
                    <th>@Html.DisplayNameFor(x => Model.tytul)</th>
                    <th>@Html.DisplayNameFor(x => Model.kategoria.nazwa)</th>
                    <th>@Html.DisplayNameFor(x => Model.liczba_ocen)</th>
                    <th>@Html.DisplayNameFor(x => Model.avg_ocena)</th>
                    <th>@Html.DisplayNameFor(x => Model.typ)</th>
                </tr>
                <tr>
                    <td>@Model.tytul</td>
                    <td>@ViewData["kategoria"]</td>
                    <td>@Model.liczba_ocen</td>
                    <td>@Model.avg_ocena</td>
                    <td>@Model.typ</td>
                </tr>
            </table>
        </div> 
 <div>
                @using (Html.BeginForm("Confirm", "Wydawca", new { app = @Model }))
                {
                    <input type="submit" value="Cofirm it" />
                }
            </div>

At the end button "Confirm it" is created and once you click it invokes Confirm Method but app variable is always null. If I set its value to anything but Model it works.

    [HttpPost]
    public ActionResult Confirm(aplikacja app)
    {
        ...
    }

While creating button "Confirm it" Model is not null, I checked. Do you happen to know what is going wrong?

Generated html

   <form action="/Wydawca/Confirm?app=Adds.Models.aplikacja" method="post">      
   <input type="submit" value="Zatwierdź" />

Upvotes: 0

Views: 15804

Answers (2)

DavidG
DavidG

Reputation: 119017

The Html.BeginForm should wrap around all of your input elements or it has nothing to post. Change your view to this:

@if (Model != null)
{
    @using (Html.BeginForm("Confirm", "Wydawca", new { app = @Model }))
    {
        <div id="appInfo">
            <table>
                <tr>
                    <th>@Html.DisplayNameFor(x => Model.tytul)</th>
                    <th>@Html.DisplayNameFor(x => Model.kategoria.nazwa)</th>
                    <th>@Html.DisplayNameFor(x => Model.liczba_ocen)</th>
                    <th>@Html.DisplayNameFor(x => Model.avg_ocena)</th>
                    <th>@Html.DisplayNameFor(x => Model.typ)</th>
                </tr>
                <tr>
                    <td> @Model.tytul</td>
                    <td>@ViewData["kategoria"]</td>
                    <td>@Model.liczba_ocen</td>
                    <td>@Model.avg_ocena</td>
                    <td>@Model.typ</td>
                </tr>
            </table>
        </div> 
        <div>
            @Html.HiddenFor(x => Model.tytul)
            @Html.HiddenFor(x => Model.kategoria.nazwa)
            @Html.HiddenFor(x => Model.liczba_ocen)
            @Html.HiddenFor(x => Model.avg_ocena)
            @Html.HiddenFor(x => Model.typ)
            <input type="submit" value="Cofirm it" />
        </div>
    }
}

Upvotes: 5

echo
echo

Reputation: 7875

You are attempting to pass an object (@Model) as a route parameter (app). Route parameters should contain scalar values (int, string, etc), not objects. View the generated HTML source in the browser and see what the <form action=""> is being set as. Then review the concept of model binding.

Upvotes: 2

Related Questions