Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > BOOK: Professional ASP.NET MVC 1.0 ISBN: 978-0-470-38461-9
|
BOOK: Professional ASP.NET MVC 1.0 ISBN: 978-0-470-38461-9
This is the forum to discuss the Wrox book Professional ASP.NET MVC 1.0 by Rob Conery, Scott Hanselman, Phil Haack, Scott Guthrie; ISBN: 978-0-470-38461-9
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET MVC 1.0 ISBN: 978-0-470-38461-9 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 2nd, 2012, 07:16 AM
Authorized User
 
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>
 
Old July 24th, 2013, 01:18 AM
Authorized User
 
Join Date: Jun 2013
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That is pretty much complicated i guess..
__________________
REI Coupon





Similar Threads
Thread Thread Starter Forum Replies Last Post
Attach Document through Blog post (Create a Post). Ranjan Kumar BOOK: Workflow in SharePoint 2010: Real World Business Workflow Solutions 0 December 2nd, 2011 11:08 AM
Using Html.BeginForm helper method tedr BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 10 May 28th, 2010 09:03 PM
Pass value to second page (create & pass var) ismailc ASP.NET 2.0 Basics 8 April 24th, 2010 07:03 AM
.NET model vs. pre .NET model. raychoudhury BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 0 January 11th, 2007 06:11 AM
Get Model Property graveman Pro VB 6 0 March 10th, 2004 12:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.