简体   繁体   中英

How to create.cshtml when button is invoked on your MVC web application?

Who can help me, i have a create button on my View layout for login, it has this fields username and password, login button(this validates if the user exist) and create(this i am struggling to link it).

I have this on my AppStart , RouteConfig.cs logic below; The question must i need to create that logic on this file(Create page functionality on RouteConfig.cs )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace eNtsaPortalWebsiteProject
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
            );
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace eNtsaPortalWebsiteProject
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Account", action = "Create", id = UrlParameter.Optional }
            );
        }
    }
}

Views/Account/Create.cshtml page something that will link this route to handle load from the web? I don't know please assist me, as i still new to MVC web development.

Logic for login on Views/Account/login.cshtml :

@model eNtsaPortalWebsiteProject.Models.Login

@{
    ViewBag.Title = "Login";
}

@{
        Layout = "~/Views/Shared/_LoginLayout.cshtml";
}

<script src="~/Scripts/jquery-1.9.1.js"></script>
<script src="~/bootstrap/js/bootstrap.min.js"></script>
<script src="~/Scripts/jquery.min.js"></script>
<link href="~/bootstrap/css/bootstrap.min.css" rel="stylesheet" />

<div data-="mainContent">
    <section class="container">
        <div class="logo col-sm-12 text-center col-md-12"> <img alt="" src="~/Images/eNtsa.png" /></div>
        <div class="clearfix"></div>

        <div class="container">
            <div class="row">
                <div id="MyWizard" class="formArea LRmargin">
                    @using (Html.BeginForm())
                    {
                        @Html.AntiForgeryToken()

                        <div id="divMessage" class="text-center col-md-12 col-md-offset-12 alert-success">
                            @Html.ValidationSummary()
                        </div>
                        <div class="glyphicon-log-out col-sm-12 text-center col-md-12"> <img alt="" src="~/Images/gcobani.jpg" /></div>
                        <div class="clearfix"></div>
                        <div class="col-md-12 col-md-offset-10 col-xs-12">
                            <div class="loginPage panel-info">
                                <span class=""><i class="glyphicon glyphicon-user">Username</i></span>
                                <div class="form-group text-center">
                                    @Html.TextBoxFor(model => model.username, new { @class = "form-control text-center", autocomplete = "off" })
                                    @Html.ValidationMessageFor(model => model.username)
                                </div>

                                <div class="form-group">
                                    <span class=""><i class="glyphicon glyphicon-user">Password</i></span>
                                    @Html.PasswordFor(model => model.password, new { @class = "form-control text-center", autocomplete = "off" })
                                    @Html.ValidationMessageFor(model => model.password)
                                </div>
                            </div>

                            <div class="form-group">
                                <input id="BtnLogin" type="submit" class="btn btn-success btn-pressure" name="BtnLogin" value="Login" />
                                <input id="BtnReset" type="reset" value="Create" class="btn btn-info btn-pressure" name="BtnReset" value="Reset" />
                            </div>
                        </div>
                    }
                    <div class="clear"></div>
                </div>
          </div>

Your button type for "Create" is set to "reset":

<div class="form-group">
   <input id="BtnLogin" type="submit" class="btn btn-success btn-pressure" name="BtnLogin" value="Login" />
   <input id="BtnReset" type="reset" value="Create" class="btn btn-info btn-pressure" name="BtnReset" value="Reset" />
</div>

As per W3Schools, this will reset the form to its initial values: https://www.w3schools.com/tags/att_button_type.asp

  • button: The button is a clickable button
  • submit: The button is a submit button (submits form-data)
  • reset: The button is a reset button (resets the form-data to its initial values)

Example: https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type

If you want the "Create" button to send back the form to your controller, I suggest you try changing the button type to "Submit"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM