2

Upon successful validation of the form, I'm unable to redirect the form. Please help me. I'm very new to ASP.NET and MVC Concepts. I have given below the model, the view and the controller. The Index page displays login information, and I'm submitting the form to the same page. If there are no errors, I have to redirect the form to a different page. This is what I'm trying to do. But even If I provide valid login information, the form doesn't redirect to the specified page.

Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MyProject.Models
{
  public class LoginModel
  {  
    [Required(ErrorMessage = "UserCode is Required.")]
    public string UserCode
    {
        get;
        set;
    }

    [DataType(DataType.Password)]
    [Required(ErrorMessage = "Password is Required.")]
    public string Password
    {
        get;
        set;
    }
  }
}

Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyProject.Models;

namespace MyProject.Controllers
{
[HandleError]
  public class HomeController : Controller
  {
    // GET
    public ActionResult Index()
    {
        return View();
    }

    // POST
    [HttpPost]
    public ActionResult Index(LoginModel model)
    {
        if (ModelState.IsValid)
        {
            RedirectToAction("Transfer", "Home");
        }

        return View(model);
    }

    public ActionResult UpgradeBrowser()
    {
        return View();
    }
  }
}

View

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"   Inherits="System.Web.Mvc.ViewPage<MyProject.Models.LoginModel>" %>


<div id="LoginBox">
<% using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "FrmLoginUser" }))
   { %>
    <table class="TblForm">
        <tr>
            <td><label for="UserName">UserCode</label></td>
            <td><%= Html.TextBox("UserCode", "", new { id = "UserCode" })%></td>
            <td><%= Html.ValidationMessage("UserCode", new { @class = "ValidationError" })%></td>
        </tr>
        <tr>
            <td><label for="Password">Password</label></td>
            <td><%= Html.Password("Password", "", new { id="Password" })%></td>
            <td><%= Html.ValidationMessage("Password", new { @class = "ValidationError" })%></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Login" /></td>
        </tr>
    </table>
<% } %>

</div> <!-- #LoginBox -->

1 Answer 1

2

You have to actually return the result of the RedirectToAction call. Change RedirectToAction to be return RedirectToAction in your controller HttpPost method:

[HttpPost]
public ActionResult Index(LoginModel model)
{
    if (ModelState.IsValid)
    {
        return RedirectToAction("Transfer", "Home");
    }

    return View(model);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Dude, you are awesome. Thank you so much for the help. Successfully working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.