0

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ź" />

3
  • What is the rendered html of the form tag? (not sure what you expect app = @Model to render) Commented Oct 2, 2014 at 12:02
  • The Html.BeginForm should wrap around all of your input elements or it has nothing to post. Commented Oct 2, 2014 at 12:06
  • Since you not editing any data, why do you need to post the whole model back (as opposed to say setting the route parameter to the unique ID of the model) Commented Oct 2, 2014 at 12:30

2 Answers 2

5

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>
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

There are still no named elements in the form.
Thanks, it finally kicked off working. Any link for further explanation?
What else do you need to know? It's essentially basic HTML forms.
Basically I thought that if you send Model you can send it back, so I am confused why we need hidden at all and how it keeps right information in post method?
Read the HTML output the is rendered in the browser, that should help.
2

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.

1 Comment

Your form doesn't contain any named elements, so there's nothing for the model binder to attempt to bind into the aplikacja app parameter of your action method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.