gpt4 book ai didi

c# - Razor 显示属性名称而不是值

转载 作者:行者123 更新时间:2023-11-30 13:36:55 24 4
gpt4 key购买 nike

我刚刚启动 MVC 和 Razor ,我在显示数据时遇到问题,当我尝试在 UI 上显示时, Controller 返回 View 中的集合数据列表,它显示的是属性名称而不是数据。对于 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);
}

最佳答案

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

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

关于c# - Razor 显示属性名称而不是值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28258298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com