2

So I am trying to show some text that i retrieved from the database and wanted to show them as a label or just show up on a page. I am having trouble displaying this text and here is what i have:

controller:

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);
    }

My Model:

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

My view:

    @model Adams.Models.StudentNameModel
     <fieldset>
     @Html.LabelFor(m => m.StudentName)
     @Html.TextBoxFor(m => m.StudentName)
</fieldset>
6
  • Do you want the value as a read only label or as the inital value in a textbox that the user can edit? Commented Feb 6, 2013 at 22:07
  • @glosrob i just wanted to have it as a read only label - but i will also be using the idea of a textbos for the user to edit on a different feature so it would be great to have both ways. Commented Feb 6, 2013 at 22:13
  • Put a break point in your Action method _StudentName and try debugging and check whether result has any value or not Commented Feb 7, 2013 at 5:31
  • @Karthik I did and when i debug it is not even getting to the Action method. I am lost to what is wrong or what i might be missing. Commented Feb 7, 2013 at 14:03
  • Do you have [HttpPost] on your controllers action, please check ? Also your ControllerAction is prepended with _. Is your view name is _StudentName.cshtml ? If not please your controllers action to StudentName in your controllers action to get it hit Commented Feb 7, 2013 at 14:09

1 Answer 1

4

seems you just need

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

instead of

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

by the way, change your query to something like

var studentName = (from s in from s in db.Students
                         where s.ID == id
                         select s.StudentName)
                  .FirstOrDefault();
if (student != null)
   model.StudentName = studentName;
Sign up to request clarification or add additional context in comments.

10 Comments

Tried this but nothing was displayed. Does that mean that it didn't pull anything back from the database to display?
@072et seems so, yes. You had nothing with TextBoxFor, did you ?
i tried the TextBoxFor and didn't get anything to display. I am sure that i have an item to return from the database cuz i tested the sql query - is there something wrong in my control that i am not seeing?
@072et did you try the edited version of your query ? or not ?
My query is working fine but i just feel like in my code it is not even getting to my controller to retrieve the name.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.