在运行时更改MVC 4标签文本

时间:2014-12-08 07:43:48

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

我刚开始使用MVC。我只是想模仿Label.Text ="一些字符串"在MVC中。我怎样才能做到这一点?

我试过这个

模型

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*/

}

控制器

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

查看

<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>

我继续将错误对象引用设置为对象的实例。谢谢!

0 个答案:

没有答案