剃刀显示属性名称而不是值

时间:2015-02-01 00:54:59

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

我刚启动MVC和razor,我遇到显示数据的问题,当我试图在UI上显示它显示属性名称而不是数据时,控制器返回视图中的集合数据列表。 对于exmaple @ Html.LabelFor(M => M [i] .Questions)假设显示问题数据,但它在UI中显示属性名称,我会非常感谢有人帮助我。

enter image description here

 @model List<Question.Models.Questionnaire>
   @using System.Linq
     @{
     ViewBag.Title = "Index";
      Layout = "~/Views/Shared/_Layout.cshtml";
     }

 <h2>Question List</h2>

 @using(Html.BeginForm("GetAnswer","Home")) {


for(int i = 0;i < Model.Count;i++) {

    //<text>@Model[i].Questions</text> <br />

      @Html.HiddenFor(M => M[i].QuestionID)
      **@Html.LabelFor(M =>M[i].Questions)**

    if (@Model[i].MultipleChoice == false){

        @Html.TextBoxFor(M => M[i].Response) <br />

    } else {

        for(int j = 0;j < Model[i].GetAns.Count;j++) {
        <div>
        <text>@Model[i].GetAns[j].AnsText</text>
        @Html.RadioButtonFor(M =>M[i].Questions, Model[i].GetAns[j].AnswerId)
        </div>
        }
        <br />
    }
}


     public class Questionnaire
     {
        public Questionnaire() {
      }

    public int QuestionID  { get; set;}
    public string Title    { get; set;}
    public string Questions{ get; set;}
    public string Response { get; set;}
    public string Response1 {
        get;
        set;
    }
    public bool MultipleChoice { get; set;}

    public List<Answer>GetAns { set; get;}

}


 public class QuestionRepository
 {

    public List<Questionnaire>
         GetQuestionnaire() {
             List<Questionnaire> q = new List<Questionnaire>();
             q.Add(new Questionnaire() {
                 QuestionID = 11, Title = "Geo", Questions = "Capital of England?", GetAns = GetAns(), MultipleChoice = false, Response1="TEST"
             });
             q.Add(new Questionnaire() {
                 QuestionID = 22, Title = "Geo", Questions = "Capital of France", GetAns = GetAns(), MultipleChoice = false, Response1 = "TEST2"
             });
             q.Add(new Questionnaire() {
                 QuestionID = 33, Title = "Geo", Questions = "Capital of Cuba", GetAns = GetAns(), MultipleChoice = true, Response1 = "TEST3"
             });
             return q;
    }

    public List<Answer> GetAns() {

        List<Answer> ans = new List<Answer>();
        ans.Add(new Answer() { AnswerId = 1, AnsText = "london", Ques = new Questionnaire() { QuestionID = 11 } });
        ans.Add(new Answer() { AnswerId = 2, AnsText = "paris", Ques = new Questionnaire() {  QuestionID = 22 } });
        ans.Add(new Answer() { AnswerId = 3, AnsText = "Havana", Ques = new Questionnaire() { QuestionID = 33 }  });

        return ans;
    }



}


   public ActionResult Index()
    {

        var q = new QuestionRepository().GetQuestionnaire();

        return View(q);
    }

2 个答案:

答案 0 :(得分:5)

@Html.LabelFor()用于显示属性的名称(或[Display(Name="...")]定义的值),而不是属性的值。如果要显示该值,请使用

@Html.DisplayFor(m =>m[i].Questions)

答案 1 :(得分:4)

这就是Html.LabelFor()的作用。这意味着在输入元素旁边创建一个标签,其中标签具有字段名称,输入元素具有值。

如果您想要输入元素中的值,请改用Html.TextBoxFor()之类的内容。或者您可以使用Html.DisplayFor()创建某种显示元素。或者,如果您只想要将值发送到标记,只需直接发出它:

@model.SomeProperty

您不需要使用HTML帮助程序方法直接发出值,只需将标记的那部分直接绑定到模型。