4 Replies - 17492 Views - Last Post: 14 May 2013 - 02:17 PM

#1 ItIntern3  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 97
  • Joined: 14-July 10

MVC View not passing Data to Controller on Postback

Posted 12 January 2013 - 02:24 PM

Hi all,

I am having difficulty getting my MVC app to pass data from the view back to its controller.

I am using a shared view-model which has a few fields that are recognized by the controller. I can assign values to the speific members of the view-model class, and pass the data from the controller to the view, and the view recognizes the data and is able to make decisions on it, however, I do not know why when the user hits a button:

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


on the view, which is located within a form:
 <form action ="" method="post"> </form>


the controller, does not recognize the data.

I have the following clause specifying the model to use in my view:
@model LSWA.Models.SignInSignOutModel
@{
}


and the following controller method header:
[HttpPost]
        public ActionResult SignOut(LearningServicesWebApp.Models.TutorSignInSignOutModel model)


I am not manipulating any data within my view, but I would like the controller to recognize the data that is being passed from the view when the user hits the submit button on my view. Again, when the user hits the post button the "model" variable of type "LearningServicesWebApp.Models.TutorSignInSignOutModel" is null and I don't know why.

Any help with this would be very helpful, thank you in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: MVC View not passing Data to Controller on Postback

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,575
  • Joined: 12-December 12

Re: MVC View not passing Data to Controller on Postback

Posted 12 January 2013 - 03:42 PM

Do you not need to supply an action for the form? Even "#" perhaps.
Was This Post Helpful? 0
  • +
  • -

#3 ItIntern3  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 97
  • Joined: 14-July 10

Re: MVC View not passing Data to Controller on Postback

Posted 12 January 2013 - 04:21 PM

If I pass in a "#" symbol into the form action the view still does not return the view model back to the controller.

Here is the entire view code:

This may help to diagnose something?

@model LearningServicesWebApp.Models.TutorSignInSignOutModel
@{

}

@Html.ValidationSummary()


@if (IsPost && @Model.WeeklySession == true && @Model.signedOut == true) @*This is executed on the post back and I am suspecting something in this if statement is rendering the view model unreadable by the controller.*@
{
    <h2>Reschedule Your Weekly Session</h2>
    <p>
        Would you like to schedule another session at the same time next week? If the same time is desired, click yes.            
    </p>
    <form action ="#" method="post">
    <table>
        <tr>
            <td><input type="radio" name="occurNextWeek" value = "true" checked = "checked" />Yes</td>
        </tr>

        <tr>
            <td>Change Session time next week (ex: 11:50):</td>
            <td><input type="text" name="RescheduleSessionTime"/></td>
            <td>@Html.ValidationMessage("RescheduleSessionTime", "*")</td>
            <td><input type="radio" name="RescheduleAMPM"/>AM <input type="radio" name="RescheduleAMPM" value = "PM" checked = "checked"/>PM</td>

        </tr>        
    </table>
    <table>
        <tr>
            <td><input type="radio" name="occurNextWeek" value = "false" checked = "checked"/>Do not reschedule session</td>
        </tr>
    </table> 
    <input type = "submit" value = "Submit"/>  
    </form>        
}
 @*If the session is not weekly, sign the user out*@
else if (IsPost && @Model.WeeklySession == false)
{
       <p>
            You have been signed out of your session. Thank you. 
       </p>
}

else @*This bit of code is executed when the view is first called*@
{
    <h2>Sign out from your session</h2>
    <form action ="#" method="post">
    <table>
        <tr>
            <td>SessionID: </td>
            @if (IsPost)
            {               
                <td>
                    <input type="text" name = "SessionID"  value = "@Model.SessionID" size="10"/>                    
                    @Html.ValidationMessage("SessionID", "*")
                </td>
            }
            else
            {
                <td>
                    <input type="text" name = "SessionID" size="10"/>                    
                    @Html.ValidationMessage("SessionID", "*")
                </td> 
            }
        </tr> 
    </table>
    <input type = "submit" value = "Submit" />  
    </form>                  
}


This post has been edited by ItIntern3: 12 January 2013 - 04:29 PM

Was This Post Helpful? 0
  • +
  • -

#4 mennunav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-January 13

Re: MVC View not passing Data to Controller on Postback

Posted 31 January 2013 - 10:20 PM

I don't know whether you came across "@using (Html.BeginForm())" that creates a form around ur page.

usage :

@using (Html.BeginForm()){

... form inputs ...

}


View Postmennunav, on 31 January 2013 - 10:16 PM, said:

I don't know whether you came across "@using (Html.BeginForm())" that creates a form around ur page.
It binds the data to the GET model and post it to the same Method of Post type

usage :

@using (Html.BeginForm()){

... form inputs ...

}

Was This Post Helpful? 0
  • +
  • -

#5 ItIntern3  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 97
  • Joined: 14-July 10

Re: MVC View not passing Data to Controller on Postback

Posted 14 May 2013 - 02:17 PM

Thank you for your help, sorry for my late reply, but I have solved the problem. When I first called the view method from the controller (I was lacking the [HttpGet]) clause in front of my controller code. I was also calling the view incorrectly as my code
return view()
should have been
return view(model)
passing the view an empty model allowed me to populate the model as the user input was entered. When the user clicked the submit button, the model was appropriately passed to the controller with the clause
[HttpPost]
before the signature. The entire signature of the controller is seen below:

 [HttpPost]
        public ActionResult TutorSignOut(ModelType model)



I did not have to use the
@using (Html.BeginForm())
method listed above.

Thanks all for your time!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1