2

所以我试图显示一些我从数据库中检索到的文本,并希望将它们显示为标签或只是显示在页面上。我无法显示此文本,这就是我所拥有的:

控制器:

public ActionResult _StudentName(int id)
    {
        id = 12;
        var model = new StudentNameModel();
        using (var db = new School())
        {
            var result = from s in db.Students
                         where s.ID == id
                         select s.StudentName;
            model.StudentName = result.ToString();
        }
        return View(model);
    }

我的模型:

public class StudentNameModel
{
    [Display(Name = "Student Name")]
    public string StudentName { get; set; }
}

我的观点:

    @model Adams.Models.StudentNameModel
     <fieldset>
     @Html.LabelFor(m => m.StudentName)
     @Html.TextBoxFor(m => m.StudentName)
</fieldset>
4

1 回答 1

3

看来你只需要

@Html.DisplayFor(m => m.StudentName)

代替

@Html.TextBoxFor(m => m.StudentName)

顺便说一句,将您的查询更改为类似

var studentName = (from s in from s in db.Students
                         where s.ID == id
                         select s.StudentName)
                  .FirstOrDefault();
if (student != null)
   model.StudentName = studentName;
于 2013-02-06T22:06:26.770 回答