Solved

error when updating forms with 'mvc'

Posted on 2014-09-10
5
190 Views
Last Modified: 2014-09-17
I have forms on a /check/confirm.cshtml page with two buttons, 'Update' & 'Confirm'
When I click on the Update button, it does not update the form, but is transferred to page
/checkout/completed.cshtml
For clarity the complete code is below:
Thanks in advance for any help given.
@model CheckoutConfirmModel
@using Nop.Core.Domain.Orders;
@using Nop.Web.Models.Checkout;
@using Nop.Web.Models.Common;
@using Nop.Web.Models.ShoppingCart;
@{
    Layout = "~/Views/Shared/_ColumnsThree.cshtml";
    ViewBag.Title = "Create";
    Int32 nCount = Convert.ToInt32(Session["nCount"]);
    var result = "";
    if (IsPost)
    {
        var firstName = Request["FirstName"];
        var lastName = Request["LastName"];
        var email = Request["Email"];
        var userData = firstName + "," + lastName +
            "," + email + Environment.NewLine;
        var dataFile = Server.MapPath("~/App_Data/data" + @Session["CustId"] + ".txt");
        File.AppendAllText(@dataFile, userData);
        result = "Information saved.";
    }
}
<div id="page shopping-cart-page" style="border:0px solid red; width:680px; height:130px; background-color: white; margin-left:15px;">
    @Html.Action("CheckoutProgress", "Checkout", new { step = CheckoutProgressStep.Cart })
    <div class="page-title"><br />
        <h2>Your&nbsp;@T("ShoppingCart")</h2>
        <span id="spanEnquiry" style="position:absolute;top:265px;left:550px;color:Black;" >
        <b>Thank you for enquiring about this product.</b></span>
        </div>
</div>
<h2>&nbsp;&nbsp;&nbsp;&nbsp;Order</h2>
@using (Html.BeginRouteForm("CheckoutConfirm", "Confirm", FormMethod.Post, new { id = "confirm-order-form" }))
{
    @*Based on your object to change the Id to the Corresponding property name*@
    @Html.HiddenFor(Model => Model.id)
    <div id="fldset" style=" position:relative; background-color:White; left:15px; width:680px;">
    <br />
    <fieldset>
        <legend>Information</legend>
                <p>Please give us a little more information about your printing / embroidery requirements<br />               
                Any additional information on the garments you want us to have?<br />
                Only complete the following information once you have finished adding products<br /> 
                to your enquiry and are ready to submit<br />
                </p>
                <p>
                Please give us a little more information about your printing / embroidery requirements<br />
                (e.g. position of the logo, sizing, number of colours, if plain text is required...): *
                </p>  
        <input type="submit" name="nextstep" value="@T("Checkout.ConfirmButton")" class="Button3" /><br />    
    </fieldset>
    </div>
}
            <div class="editor-field" style=" position:relative; background-color:White; left:15px; width:680px;">
            <fieldset>
            <legend>Information</legend>     
           <form id="form1" method="post" action="Confirm" >
           @*Based on your object to change the Id to the Corresponding property name*@
           @Html.HiddenFor(Model => Model.id)
           <table>
            <tr>
                <td>First Name:</td>
                <td><input id="FirstName" name="FirstName" type="text" /></td>
            </tr>
            <tr>
                <td>Last Name:</td>
                <td><input id="LastName" name="LastName" type="text" /></td>
            </tr>
            <tr>
                <td>Email:</td>
                <td><input id="Email" name="Email" type="text" /></td>
            </tr>
            <tr>
                <td>@Html.ActionLink("Go to Display", "", "Checkout/Completed")</td>
                <td><input type="submit"  value="Update" class="Button3"/></td>
            </tr>
        </table>
        </form>
        </fieldset>
        </div>
        <div style=" position: relative; top:0px; left:15px; width:705px;width:680px; background-color:White; color:Maroon; font-size:small;">
        <fieldset>
        <legend>Information</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.OrderEnquiry)
        </div>
        <div class = "order-enquiry">
            AddInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:@Session["AdditionalInformation"]#<br />
            CustomeId&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:@Session["CustId"]#<br />
         </div>
            
            @if (result != "")
            {
            <p>Result: @result</p>
            }
        </fieldset>
        </div>

Open in new window

0
Comment
Question by:homeshopper
  • 4
5 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
What do you mean by "update the form?" What is it updating? As you've written it, the update button is a submit-type <input>, so it will post the form when you click it.
0
 

Author Comment

by:homeshopper
Comment Utility
Hi Kaufmed,
Just to clarify the confusion, there are two code blocks below.
The first code block is the confirm page as it was originally.
It has a confirm button that transfers the page to the completed page.
The second code block is what I called an update page.
It has a button that should update or append the content of the textboxes
to a text file named as the customerId and stay on the same page,
but it does not, it just transfers the confirm page to the completed page
and the text file is not updated. I also wanted to conbine it all as one page.
Thanks in adavnce for any help given.
Best Regards, Ian.
@model CheckoutConfirmModel
@using Nop.Core.Domain.Orders;
@using Nop.Web.Models.Checkout;
@using Nop.Web.Models.Common;
@using Nop.Web.Models.ShoppingCart;
@{
    Layout = "~/Views/Shared/_ColumnsThree.cshtml";
    ViewBag.Title = "Create";
    Int32 nCount = Convert.ToInt32(Session["nCount"]);
}
<div id="page shopping-cart-page" style="border:0px solid red; width:680px; height:130px; background-color: white; margin-left:15px;">
    @Html.Action("CheckoutProgress", "Checkout", new { step = CheckoutProgressStep.Cart })
    <div class="page-title"><br />
        <h2>Your&nbsp;@T("ShoppingCart")</h2>
        <span id="spanEnquiry" style="position:absolute;top:265px;left:550px;color:Black;" >
        <b>Thank you for enquiring about this product.</b></span>
        </div>
</div>
<h2>&nbsp;&nbsp;&nbsp;&nbsp;Order</h2>
@using (Html.BeginRouteForm("CheckoutConfirm", "Confirm", FormMethod.Post, new { id = "confirm-order-form" }))
{
    @*Based on your object to change the Id to the Corresponding property name*@
    @Html.HiddenFor(Model => Model.id)
    <div id="fldset" style=" position:relative; background-color:White; left:15px; width:680px;">
    <br />
    <fieldset>
        <legend>Information</legend>
                <p>Please give us a little more information about your printing / embroidery requirements<br />               
                Any additional information on the garments you want us to have?<br />
                Only complete the following information once you have finished adding products<br /> 
                to your enquiry and are ready to submit<br />
                </p>
                <p>
                Please give us a little more information about your printing / embroidery requirements<br />
                (e.g. position of the logo, sizing, number of colours, if plain text is required...): *
                </p>  
        <input type="submit" name="nextstep" value="@T("Checkout.ConfirmButton")" class="Button3" /><br />    
    </fieldset>
    </div>
}

Open in new window

@model CheckoutConfirmModel
@using Nop.Core.Domain.Orders;
@using Nop.Web.Models.Checkout;
@using Nop.Web.Models.Common;
@using Nop.Web.Models.ShoppingCart;
@{
    var result = "";
    if (IsPost)
    {
        var firstName = Request["FirstName"];
        var lastName = Request["LastName"];
        var email = Request["Email"];
        var userData = firstName + "," + lastName +
            "," + email + Environment.NewLine;
        var dataFile = Server.MapPath("~/App_Data/data" + @Session["CustId"] + ".txt");
        File.AppendAllText(@dataFile, userData);
        result = "Information saved.";
    }
}
<div class="editor-field" style=" position:relative; background-color:White; left:15px; width:680px;">
            <fieldset>
            <legend>Information</legend>     
           <form id="form1" method="post" action="Confirm" >
           @*Based on your object to change the Id to the Corresponding property name*@                  
           @Html.HiddenFor(Model => Model.id)                   
           <table>
            <tr>
                <td>First Name:</td>
                <td><input id="FirstName" name="FirstName" type="text" /></td>
            </tr>
            <tr>
                <td>Last Name:</td>
                <td><input id="LastName" name="LastName" type="text" /></td>
            </tr>
            <tr>
                <td>Email:</td>
                <td><input id="Email" name="Email" type="text" /></td>
            </tr>
            <tr>
                <td>@Html.ActionLink("Go to Display", "", "Checkout/Completed")</td>
                <td><input type="submit"  value="Update" class="Button3"/></td>
            </tr>
        </table>
        </form>
        </fieldset>
        </div>
        <div style=" position: relative; top:0px; left:15px; width:705px;width:680px; background-color:White; color:Maroon; font-size:small;">
        <fieldset>
        <legend>Information</legend>
        <div class="editor-label">  
        @Html.LabelFor(model => model.OrderEnquiry)     
        </div>
        <div class = "order-enquiry">
            AddInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:@Session["AdditionalInformation"]#<br />
            CustomeId&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:@Session["CustId"]#<br />
         </div>
            
            @if (result != "")
            {
            <p>Result: @result</p>
            }
        </fieldset>
        </div>

Open in new window

0
 

Author Comment

by:homeshopper
Comment Utility
I have found a solution myself as follows.
Changed <form id="form1" method="post" action="Confirm" >
to            <form id="form1" method="post" action="Confirm" >
Then in the CheckoutController.cs new routine as follows based on the Confirm routine,
but without redirect code etc.
[HttpPost, ActionName("txtConfirm")]
        [ValidateInput(false)]
        public ActionResult ConfirmOrder2()
        {
            //validation
            //
            //return RedirectToRoute("CheckoutCompleted");
              return View(model);
        }

Open in new window

0
 

Accepted Solution

by:
homeshopper earned 0 total points
Comment Utility
Typo in post above, should be <form id="form1" method="post" action="txtConfirm" >
[HttpPost, ActionName("txtConfirm")]
        [ValidateInput(false)]
        public ActionResult ConfirmOrder2()
        {
            //validation
            //
            //return RedirectToRoute("CheckoutCompleted");
              return View(model);
        }

Open in new window

0
 

Author Closing Comment

by:homeshopper
Comment Utility
I found the solution myself
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Read about how to choose the best possible content marketing agency to suit your needs. Content marketing has become an integral part of running a successful tech business, so it is wise to be informed.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now