1

I just started using MVC. I simply want to emulate Label.Text = "some string" in MVC. How can I achieve this?

I tried this

Model

public class UserLogin
{
    [Required]
    [Display(Name = "Username")]
    public string UserName { get; set; }

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

    public string ErrorMsg { get; set; } /*I want to dynamically change the value of the error message*/

}

Controller

 public ActionResult Index(UserLogin model, string returnUrl)
    {
    model.ErrorMsg="Invalid Login"
    return View(model)
    }

View

<div class="pure-control-group">
            @Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
            @Html.ValidationMessageFor(m => m.UserName)
            @Html.ValidationMessageFor(m => m.Password)


            @Html.Label(Model.ErrorMsg);


        </div>

I keep on getting the error Object reference not set to an instance of an object. Thanks!

cafekun
  • 158
  • 2
  • 13
  • [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Soner Gönül Dec 08 '14 at 07:45
  • 4
    I suspect you just want `@Html.DisplayFor(m => m.ErrorMsg)` –  Dec 08 '14 at 07:47
  • @StephenMuecke Thank you very much! This solved my problem. I'll go and read all of the Razor methods available. – cafekun Dec 08 '14 at 07:51

0 Answers0