Note that there are some explanatory texts on larger screens.

plurals
  1. POsaving text box string value to database c#
    primarykey
    data
    text
    <p>I have a textbox in which the user can enter their desired username and save it. Once they save it and they happen to revisit their profile page that textbox should be populated with the last username they saved to display and the user will still have the ability to change it and resave. I am fairly new to this and not sure how to start this properly. I am using vs 2012 asp.net mvc 4 c#. Here is my code so far: </p> <pre><code> @model School.Models.StudentNameModel @using (Html.BeginForm("_StudentNamePartial", "Profile")) { @Html.AntiForgeryToken() @Html.ValidationSummary() &lt;fieldset&gt; &lt;ol&gt; &lt;li&gt; @Html.LabelFor(m =&gt; m.StudentName) @Html.DisplayFor(m =&gt; m.StudentName) @Html.TextBoxFor(m=&gt;m.StudentName) &lt;button type="button" value="save" /&gt; &lt;/li&gt; &lt;/ol&gt; &lt;/fieldset&gt; </code></pre> <p>}</p> <p>This is my Model: </p> <pre><code> public class StudentNameModel { [Display(Name = "Student Name")] public string StudentName{ get; set; } } </code></pre> <p>My controller:</p> <p>GET - To get the student name from database if one exists. </p> <pre><code>[HttpPost] public ActionResult _StudentNamePartial(int id) { id = WebSecurity.CurrentStudentId; var model = new StudentNameModel(); using (var db = new StudentsDataContext()) { var result = (from u in db.Students where u.ID == id select u.StudentName).FirstOrDefault(); if(result != null) model.StudentName= result; } return View(model); } </code></pre> <p>POST - This is where i want to save the new username for the student</p> <pre><code>[HttpPost] public ActionResult _StudentNamePartial(StudentNameModel model) { if (ModelState.IsValid) { using (var db = new StudentDataContext()) { try { } catch (Exception) { throw; } } return RedirectToAction("ProfileAccount"); } return View(model); } </code></pre> <p>Also i am having trouble that when i am displaying the username it is not hitting my <code>Action</code> method and it always reports that the Object reference is null. Any help will be great. Thanks :D</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload