Click here to Skip to main content
15,567,466 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Razor MVC 5 form pushing data to a sql database. When i hit submit the form just reloads and the form values are retained. Checking in the database i see that no values are committed.
I have the POST version of the ActionResult already decorated with [HttpPost]. Additionally in the view i have explicitly specified FormMethod.Post.

This is my Bank.cshtml :

C#
model MY_SYSTEM.Models.Employee.Registration
@{
    ViewBag.Title = "Bank";
    Layout = "~/Views/Shared/_LayoutPageh.cshtml";
}

@using (Html.BeginForm("Bank", "Employee", FormMethod.Post, new { @class = "form-horizontal" }))
{
    @Html.AntiForgeryToken()
    <div class="main-content-inner">
        <div class="page-content">
            <div class="page-header">
                <h1>Employee Bank Detail</h1>
            </div>
            <div class="error">@ViewBag.ErrorMessage</div>
            <div class="row">
                <div class="col-xs-12">
                    <div>
                        <div class="user-profile row">
                            <div class="col-sm-offset-1 col-sm-8">
                                <form class="form-horizontal">
                                    <div class="tabbable">
                                        <div class="tab-content profile-edit-tab-content">
                                            <div id="edit" class="tab-pane in active">
                                                <div class="row">
                                                    <div class="col-xs-12 col-sm-8">
                                                        <div class="form-group">
                                                            <label class="col-sm-3 control-label no-padding-right" for="form-field-facebook">Bank</label>

                                                            <div class="col-sm-9">
                                                                <span class="input-icon">

                                                                    @*@Html.DropDownList("BankId", new SelectList(Model.Banks, "BankId", "BankName"),"-----Select Category-----")*@
                                                                    @Html.DropDownListFor(m => m.BankId, Model.Banks, "Please Select", htmlAttributes: new { @class = "form-control" })
                                                                    @Html.ValidationMessageFor(model => model.BankId, "", new { @class = "text-danger" })
                                                                    class="ace-icon fa fa-bank blue">
                                                                </span>
                                                            </div>
                                                        </div>
                                                        <div class="space-4"></div>
                                                        <div class="form-group">
                                                            <label class="col-sm-3 control-label no-padding-right" for="form-field-facebook">Account Name</label>

                                                            <div class="col-sm-9">
                                                                <span class="input-icon">
                                                                    @Html.EditorFor(model => model.AccountName, new { htmlAttributes = new { @class = "form-control", @placeholder = "" } })
                                                                    @Html.ValidationMessageFor(model => model.AccountName, "", new { @class = "text-danger" })
                                                                    ^__i class="ace-icon fa fa-user blue">
                                                                </span>
                                                            </div>
                                                        </div>
                                                        
                                                        <div class="space-4"></div>
                                                       
                                                            <div class="">
                                                                <button type="submit" class="btn btn-sm btn-success">
                                                                    Submit
                                                                    ^__i class="ace-icon fa fa-arrow-right icon-on-right bigger-110">
                                                                </button>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                </form>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>

}


My ActionResult methods are as below:
//GET
C#
public ActionResult Bank ()
        {
            Models.Employee.Registration bank = new Models.Employee.Registration();
          
            bank.Banks = PopulateBank();
            bank.SortCodes = PopulateSortCode();       
            bank.AccountTypes = PopulateAccountType();

            return View(bank);
        }


//POST
C#
[HttpPost]
        public ActionResult Bank(Models.Employee.Registration bank)
        {
            if (!ModelState.IsValid)
            {
                bank.Banks = PopulateBank();
                bank.SortCodes = PopulateSortCode();
                bank.AccountTypes = PopulateAccountType();
                return View(bank);
            }

            try
            {       ...   cmd.ExecuteNonQuery();
                }

                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                bank.Banks = PopulateBank();
                bank.SortCodes = PopulateSortCode();
                bank.AccountTypes = PopulateAccountType();

                ViewBag.ErrorMessage = Helpers.Messages.GENERAL_ERROR;
                return View(bank);
            }
        }


What I have tried:

When i hit submit no error is being returned, it just reloads. In the Chrome Developer Tools -- Network i dont see anything pushed in data. The traffic just relates to JS, Css etc. I have also checked the Console and no meaningful messages are returned except the following (which seems not really significant, unless im missing its significance) :
Uncaught Error: Invalid dimensions for plot, width = null, height = null
at b.resize (jquery.flot.min.js:8)
at new b (jquery.flot.min.js:7)
at t (jquery.flot.min.js:7)
at new c (jquery.flot.min.js:8)
at Function.a.plot (jquery.flot.min.js:8)
at drawPieChart (mihr20:615)
at HTMLDocument.<anonymous> (mihr20:643)
at j (jquery-2.1.4.min.js:2)
at Object.fireWith [as resolveWith] (jquery-2.1.4.min.js:2)
at Function.ready (jquery-2.1.4.min.js:2)
Posted
Updated 13-Nov-19 6:21am

1 solution

Quote:
Razor
@using (Html.BeginForm("Bank", "Employee", FormMethod.Post, new { @class = "form-horizontal" }))
{
    ...
    <form class="form-horizontal">
        ...
    </form>
    ...
}
Nested forms are not supported in HTML. Try removing that extra <form> tag, or changing it to a <div>
 
Share this answer
 
Comments
Tshumore 13-Nov-19 11:37am    
I have removed . The form attempts a PostBack but still reloads back with form values filled in. In the database table its not committing anything still
Richard Deeming 13-Nov-19 11:41am    
Then it's either failing the validation, or hitting the catch block.

Try adding a validation summary within the form - @Html.ValidationSummary().

If it still doesn't work, set a breakpoint at the start of your [HttpPost] method and step through it to see where the problem is.
F-ES Sitecore 13-Nov-19 11:42am    
Step through the code in the debugger. Does your post action get hit? Are the model values what you expect? is the ModelState valid? Is "cmd" being populated as you expect? Remember we don't know what your inputs are, we can't access your database, we can't remotely debug for you, you need to debug yourself to at least find out at what point in the process it fails and from there you can ask about solutions.
Tshumore 13-Nov-19 11:54am    
ok thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900