使用ASP.NET MVC中的ViewModel在索引视图上登录和注册

时间:2016-06-09 07:42:09

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我想在索引视图中添加注册表单和登录表单,并在我的MVC项目中登录时显示(工作)注销按钮(最好只是一个锚元素)。我复制了一些现有的默认登录代码,并希望将一些现有的默认注册码(均在默认AccountController中找到)添加到我的HomeController。我目前的代码如下:

HomeController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using Rent_a_Car.Filters;
using Rent_a_Car.Models;

namespace Rent_a_Car.Controllers
{
    [InitializeSimpleMembership]
    public class HomeController : Controller
    {
        public ActionResult Index(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;

            return View();
        }

        // POST: /
        // I think on Post I need to decide wether the register form or login form is returned from the view
        // And make some methods that handle these forms, but I do not understand how to do this within MVC
        // This is how far I came to understand on how to add at least a login and probably should change the
        // name of the method to make it more understandable to what the code actually is supposed to do?

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Index(LoginRegisterViewModel model, string returnUrl)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.Login.UserName, model.Login.Password, persistCookie: model.Login.RememberMe))
            {
                return RedirectToLocal(returnUrl);
            }

            ModelState.AddModelError("", "De ingevulde gebruikersnaam of wachtwoord is onjuist ingevoerd.");
            return View(model);
        }

        private ActionResult RedirectToLocal(string returnUrl)
        {
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        /*
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
         */
    }
}

我的Index.cshtml观点:

@model Rent_a_Car.Models.LoginRegisterViewModel

@{
    ViewBag.Title = "Home Page";
}

<div class="registrate">
    <h2>Rent-a-Car</h2>
    <h4>Registreer</h4>
    <hr />
    @using (Html.BeginForm()) {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary()

        <p>De volgende gegevens zijn nodig om een bestelling te kunnen plaatsen voor het huren van een auto:</p>
        <div class="form-group">
            @Html.TextBoxFor(m => m.Register.UserName, new { @class = "form-control text-box single-line", @name = "E-mail", @placeholder = "E-mail" })
        </div>
        <div class="form-group">
            @Html.PasswordFor(m => m.Register.Password, new { @class = "form-control text-box single-line", @name = "Wachtwoord", @placeholder = "Wachtwoord" })
        </div>
        <div class="form-group">
            @Html.PasswordFor(m => m.Register.ConfirmPassword, new { @class = "form-control text-box single-line", @name = "Bevestig", @placeholder = "Bevestig uw wachtwoord" })
        </div>
    }
</div>

<div class="welcome">
    <h4>Log in</h4>
    <hr />
    @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <p>Om een auto te kunnen huren moet u eerst ingelogd zijn: </p>
        <div class="form-group">
            @Html.TextBoxFor(m => m.Login.UserName, new { @class = "form-control text-box single-line", @name = "E-mail", @placeholder = "E-mail" })
            @Html.ValidationMessageFor(m => m.Login.UserName)
        </div>
        <div class="form-group">
            @Html.PasswordFor(m => m.Login.Password, new { @class = "form-control text-box single-line", @name = "Wachtwoord", @placeholder = "Wachtwoord" })
            @Html.ValidationMessageFor(m => m.Login.Password)
        </div>
        <div class="checkbox">
            @Html.CheckBoxFor(m => m.Login.RememberMe)
            @Html.LabelFor(m => m.Login.RememberMe)
        </div>
        <input type="submit" class="btn btn-default" value="Inloggen" />
    }
    <hr />
    <div class="shopping-cart">
        <h4>Winkelwagen</h4>
        <hr />
        <p>U heeft de volgende auto's geselecteerd: </p>
    </div>
</div>



<div class="catalogue">
    <h3>Huur één of meerdere beschikbare auto's</h3>
    <hr />
    <div class="row">
        <div class="col-md-3">
            <div class="catalogue-box">
                <h4>Opel</h4>
                <hr />
                <img src="#" />
                <p>Beschrijving</p>
                <span>Prijs: zoveel euro</span>
            </div>
        </div>
        <div class="col-md-3">
            <div class="catalogue-box">
                <h4>Seat</h4>
                <hr />
                <img src="#" />
                <p>Beschrijving</p>
                <span>Prijs: zoveel euro</span>
            </div>
        </div>
        <div class="col-md-3">
            <div class="catalogue-box">
                <h4>Skoda</h4>
                <hr />
                <img src="#" />
                <p>Beschrijving</p>
                <span>Prijs: zoveel euro</span>
            </div>
        </div>
        <div class="col-md-3">
            <div class="catalogue-box">
                <h4>Volkswagen</h4>
                <hr />
                <img src="#" />
                <p>Beschrijving</p>
                <span>Prijs: zoveel euro</span>
            </div>
        </div>
        <div class="row">
            <div class="col-md-3">
                <div class="catalogue-box">
                    <h4>Mercedes</h4>
                    <hr />
                    <img src="#" />
                    <p>Beschrijving</p>
                    <span>Prijs: zoveel euro</span>
                </div>
            </div>
            <div class="col-md-3">
                <div class="catalogue-box">
                    <h4>Toyata</h4>
                    <hr />
                    <img src="#" />
                    <p>Beschrijving</p>
                    <span>Prijs: zoveel euro</span>
                </div>
            </div>
            <div class="col-md-3">
                <div class="catalogue-box">
                    <h4>Renault</h4>
                    <hr />
                    <img src="#" />
                    <p>Beschrijving</p>
                    <span>Prijs: zoveel euro</span>
                </div>
            </div>
            <div class="col-md-3">
                <div class="catalogue-box">
                    <h4>Ford</h4>
                    <hr />
                    <img src="#" />
                    <p>Beschrijving</p>
                    <span>Prijs: zoveel euro</span>
                </div>
            </div>
        </div>
    </div>
    @* foreach logic *@
    @*
    @foreach (var car in Model)
    {
        @Html.DisplayFor(modelItem => car.Title)
        @Html.DisplayFor(modelItem => car.ReleaseDate)
        @Html.DisplayFor(modelItem => car.Genre)
        @Html.DisplayFor(modelItem => car.Price)
        @Html.ActionLink("Details", "Details", new { id = car.ID })
    }*@
</div>

@*
@section featured {
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>@ViewBag.Title.</h1>
                <h2>@ViewBag.Message</h2>
            </hgroup>
            <p>
                To learn more about ASP.NET MVC visit
                <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
                The page features <mark>videos, tutorials, and samples</mark> to help you get the most from ASP.NET MVC.
                If you have any questions about ASP.NET MVC visit
                <a href="http://forums.asp.net/1146.aspx/1?MVC" title="ASP.NET MVC Forum">our forums</a>.
            </p>
        </div>
    </section>
}
<h3>We suggest the following:</h3>
<ol class="round">
    <li class="one">
        <h5>Getting Started</h5>
        ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
        enables a clean separation of concerns and that gives you full control over markup
        for enjoyable, agile development. ASP.NET MVC includes many features that enable
        fast, TDD-friendly development for creating sophisticated applications that use
        the latest web standards.
        <a href="http://go.microsoft.com/fwlink/?LinkId=245151">Learn more…</a>
    </li>

    <li class="two">
        <h5>Add NuGet packages and jump-start your coding</h5>
        NuGet makes it easy to install and update free libraries and tools.
        <a href="http://go.microsoft.com/fwlink/?LinkId=245153">Learn more…</a>
    </li>

    <li class="three">
        <h5>Find Web Hosting</h5>
        You can easily find a web hosting company that offers the right mix of features
        and price for your applications.
        <a href="http://go.microsoft.com/fwlink/?LinkId=245157">Learn more…</a>
    </li>
</ol>
*@

我是否应该更改两个表单的HTML帮助程序以指向发布时表单的不同方法?

我应该如何使HomeController看起来像是单独处理这两个表单(单击登录按钮后,保持在索引视图/页面上,但显示用户已登录。单击注册按钮后,显示该帐户已被注册,最好只是在表单下面添加一个标签,以显示该消息或同样的东西)以及如何更改我的Index中的HomeController方法以显示注销按钮(例如在我的布局中)用户登录时查看或简单Index.cshtml查看)?

0 个答案:

没有答案