View Single Post
  #1 (permalink)  
Old May 2nd, 2012, 08:16 AM
bh81 bh81 is offline
Authorized User
Points: 95, Level: 1
Points: 95, Level: 1 Points: 95, Level: 1 Points: 95, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
Default How to pass a model in BeginForm() and use FormMethod.post?

I am trying to develop a widzard that takes user from one page to another, "remembering" what the user has input on first page to the next without saving.

First page: Create.aspx (with 5 fields)
Second page: EstatesUserCreate.aspx (6 fields)

When submit button is hit on the Second page (EstatesUserCreate.aspx) it then saves into the database.

Below are the codes in my controller, Create.aspx and EstatesUserCreate.aspx. It works but when you hover over the submit button of the second page (EstatesUserCreate.aspx), The URL is: ="/Home/EstatesUserCreate?Request_ID=0&RequestBy=.......;I sValid=True" method="post">

It seems to ignore my FormMethod.post in
Code:
<%using (Html.BeginForm("EstatesUserCreate", "Home", Model.Request, FormMethod.Post))
.

If I remove the Model.Request in the BeginForm
Code:
<%using (Html.BeginForm("EstatesUserCreate", "Home", FormMethod.Post))
there is no querystring, which is good, but the values entered in the first page is not being passed.

Please could someone help if I could get rid of the querystring and just have
../Home/EstatesUserCreate/ instead?

In my Controller:
Code:
        // GET: /Request/Create
        public ActionResult Create()
        {
            ViewBag.Mode = "Create";
            var user = (FCWorkRequest.Models.User)Session["CurrentUser"];

            Request request = new Request();

            request.RequestBy = user.Name();
            //request.RequestBy = UsernameHelper.FormatUserName(User.Identity.Name);
            request.RequestBy_eMail = RetriveADInfo.GetADUserInfo()[1];
            request.RequestBy_Department = user.HRProDetails().School_Department_Area;
            request.RequestBy_Location = user.HRProDetails().Workroom;
            request.RequestBy_Phone = user.HRProDetails().Internal_Phone.ToString();

            return View(new RequestFormViewModel(request));
        }


        //POST: /Request/ChooseRequestType
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ChooseRequestType(Request request)
        {
            ViewBag.Mode = "Create";
            var user = (FCWorkRequest.Models.User)Session["CurrentUser"];
            ModelState.Clear();
            if (string.IsNullOrEmpty(request.RequestBy))
                ModelState.AddModelError("request.RequestBy","Request By is required");
            if (string.IsNullOrEmpty(request.RequestBy_eMail))
                ModelState.AddModelError("request.RequestBy_eMail","Request By eMail is required");
            if (request.Request_Department_ID == 0)
                ModelState.AddModelError("request.Request_Department_ID", "Request Department is required");
            if (string.IsNullOrEmpty(request.RequestBy_Location))
                ModelState.AddModelError("request.RequestBy_Location","Request By Location is required");
            if (string.IsNullOrEmpty(request.RequestBy_Phone))
                ModelState.AddModelError("request.RequestBy_Phone","Request By Phone is required");

            if (ModelState.IsValid)
            {
		return View("EstatesUserCreate", new RequestFormViewModel(request));
            }
            else
            {
                return View("Create", new RequestFormViewModel(request));
            }
        }

        //POST: /Request/EstatesUserCreate
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EstatesUserCreate(Request request)
        {
            ModelState.Clear();
            if (request.Request_Category_ID==0)
                ModelState.AddModelError("request.Request_Category_ID", "Category By is required");
            if (request.Date_of_Ticket==null)
                ModelState.AddModelError("request.Date_of_Ticket", "Date of Ticket is required");
            if (string.IsNullOrEmpty(request.Title))
                ModelState.AddModelError("request.Title", "Title is required");

            if (ViewData.ModelState.IsValid)
            {
                    requestRepository.Add(request);
                    requestRepository.Save();
                    return RedirectToAction("Index", new { id = request.Request_ID });
            }
            else
            {
                return View(new RequestFormViewModel(request));
            }
        }

    }
In my Create.aspx

Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<FCWorkRequest.Controllers.RequestFormViewModel>" %>
<%@ Import Namespace="FCWorkRequest.Helpers" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Create New Request - RequestorDetails
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">


<% Html.RenderPartial("AdminMenu"); %>
<h2>Create New Request - RequestorDetails</h2>

<%=Html.ValidationSummary("Please correct the errors and try again") %>

<% using (Html.BeginForm("ChooseRequestType","Home", FormMethod.Post)) { %>
    <div class="modeltype-left">
        <h3>Requestor's Details</h3>
        <fieldset>
            <p>
                <label for="RequestBy">Request By:</label>
                <%= Html.TextBox("RequestBy", Model.Request.RequestBy)%>
                <%=Html.ValidationMessage("Request By", "*")%>        
            </p>  
            <p>
                <label for="RequestBy_eMail">eMail:</label>
                <%= Html.TextBox("RequestBy_eMail", Model.Request.RequestBy_eMail)%>
                <%=Html.ValidationMessage("eMail", "*")%>        
            </p>  
            <p>
                <label for="RequestBy_Department">Department:</label>
                <%= Html.TextBox("RequestBy_Department", Model.Request.RequestBy_Department)%>
                <%=Html.ValidationMessage("Department", "*")%>        
            </p>  
            <p>
                <label for="RequestBy_Location">Location:</label>
                <%= Html.TextBox("RequestBy_Location", Model.Request.RequestBy_Location)%>
                <%=Html.ValidationMessage("Location", "*")%>        
            </p>  
            <p>
                <label for="RequestBy_Phone">Phone:</label>
                <%= Html.TextBox("RequestBy_Phone", Model.Request.RequestBy_Phone)%>
                <%=Html.ValidationMessage("Phone", "*")%>        
            </p>  
        </fieldset>
    </div>

    <div class="modeltype-right">
        <h3>Request Details</h3>
        <fieldset>
            <p>
                <label for="Request_Department_ID">Department:</label>

                    <%=Html.DropDownList("Request_Department_ID",
                                                DepartmentHelper.GetRequestDepartmentsForList(), "--Select One--")%>
                    <%=Html.ValidationMessage("Request for Department", "*")%> 
            </p>
            <p>
                <button type="submit" name="save" value="save" >Next</button>
            </p>
        </fieldset>
    </div>
<% } %>

</asp:Content>
In EstatesUserCreate.aspx:

Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<FCWorkRequest.Controllers.RequestFormViewModel>" %>
<%@ Import Namespace="FCWorkRequest.Helpers" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Create Estates User Request
</asp:Content>


<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<link type="text/css" href="/Content/jquery-ui-1.8.11.custom.css" rel="stylesheet" />	

<% Html.RenderPartial("AdminMenu"); %>
<h2>Create New Request for Estates - Request Details</h2>

<%=Html.ValidationSummary("Please correct the errors and try again") %>


<%using (Html.BeginForm("EstatesUserCreate", "Home", Model.Request, FormMethod.Post))
  { %>

        <fieldset>
            <p>
                <label for="Request_Category_ID">Category:</label>

                    <%=Html.DropDownList("Request_Category_ID",
                                         CategoryHelper.GetCategoryForList(5), "--Select One--")%>
                    <%=Html.ValidationMessage("Request Category", "*")%> 
            </p>

            <script type="text/javascript" src="/Scripts/jquery-1.5.1.min.js"></script>
            <script type="text/javascript" src="/Scripts/jquery-ui-1.8.11.custom.min.js"></script>
            <script type="text/javascript" src="/Scripts/jquery-ui-timepicker-addon.js"></script>

            <script language="javascript">
                $(document).ready(function () {
                    $('#Date_of_Ticket').datetimepicker();

                    $('#Date_Required').datepicker({ dateFormat: '<%= Html.ConvertDateFormat() %>' });
                }); 
            </script> 
            <p>
                <label for="Date_of_Ticket">Date/Time of Ticket:</label>
                <%= Html.TextBox("Date_of_Ticket", Model.Request.Date_of_Ticket)%>
                <%=Html.ValidationMessage("Date_of_Ticket", "*")%>        
            </p>  
            <p>
                <label for="Date_Required">Date Required:</label>
                <%= Html.TextBox("Date_Required", Model.Request.Date_Required.ToString())%>
            </p>   
            <p>
                <label for="Title">Title:</label>
                <%= Html.TextBox("Title", Model.Request.Title)%>
                <%=Html.ValidationMessage("Title", "*")%>        
            </p>  
            <p>
                <label for="Location_Of_Job">Location of Job:</label>
                <%= Html.TextBox("Location_Of_Job", Model.Request.Location_Of_Job)%>
            </p>  
            <p>
                <label for="Description">Description:</label>
                <%= Html.TextArea("Description", Model.Request.Description, new { @class = "textarea-narrow" })%>
            </p>
            <p>
                <button type="submit" name="save" value="save">Submit</button>
            </p>
        </fieldset>
</form>
<% } %>

</asp:Content>