ConductedClever
ConductedClever

Reputation: 4305

asp.net mvc model didn't receive on form submit

I have written an asp.net mvc site that works fine on local. But after publishing it, i have some problems with models sent from forms. i think it determines my model state always not valid. i don't receive any data!

    [HttpGet]
    public ActionResult LogIn()
    {
        return View();
    }

    [HttpPost]
    public ActionResult LogIn(Models.LoginViewModel data)
    {
        if (!ModelState.IsValid)
        {
            return LogIn();
        }
        TAPortalEntities db = new TAPortalEntities();

        UserAccount probableUser = db.UserAccounts.FirstOrDefault(p => p.UserName == data.UserName && p.Pass == data.Pass);

        if (probableUser == null)
        {
            ModelState.AddModelError(string.Empty, "نام کابری یا رمز عبور اشتباه وارد شده است!");
            return LogIn();
        }

        Session["LogedIn"] = true;
        Session["UserName"] = probableUser.UserName;
        Session["Course"] = probableUser.Course.Name;
        Session["TeacherName"] = probableUser.Course.TeacherName;
        Session["RealName"] = probableUser.RealName;

        return RedirectToAction("Index", "Home");
    }

and the view is like this:

@model TAPortal.Models.LoginViewModel

@{
    ViewBag.Title = "ورود به سایت";
}

<h2>ورود</h2>


@using (Html.BeginForm()) 
{
    @*@Html.AntiForgeryToken()*@

    <div class="form-horizontal">
        <h4>اطلاعات ورود را وارد کنید</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.UserName, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.UserName)
                @Html.ValidationMessageFor(model => model.UserName)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Pass, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Pass)
                @Html.ValidationMessageFor(model => model.Pass)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="ورود" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("برگشت به خانه", "Index","Home")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

and the model class :

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

namespace TAPortal.Models
{
    public class LoginViewModel
    {
        [Required(ErrorMessage="نام کاربری باید وارد شود!")]
        [Display(Name="نام کاربری")]
        public string UserName { get; set; }

        [Required(ErrorMessage = "رمز عبور باید وارد شود!")]
        [Display(Name = "رمز عبور")]
        public string Pass { get; set; }
    }
}

see these results:

input:

input

output local :

out local

output on online host :

out online

Upvotes: 0

Views: 107

Answers (2)

ConductedClever
ConductedClever

Reputation: 4305

Now I have the answer. I share it for the others who face this problem.

From first, every thing was correct but the wrong thing was the version of the .NET framework of my host. This type of coding requires .NET framework 4.5 but my host's .NET framework version was 4.0 and it was all the story.

Now I have transferred my code to a host of 4.5.

Upvotes: 0

theLaw
theLaw

Reputation: 1291

Try to add this filter

[ValidateAntiForgeryToken]

to your POST Action

Edit:

in your get method instantiate your model and set to false the ValidationSummary

   [HttpGet]
    public ActionResult LogIn()
    {
       var model = new LoginViewModel();
        return View(model);
    }

Upvotes: 1

Related Questions