c# - ASP.NET MVC 5 don't getting values from HiddenFor in post method -
Exception of the type of 'System.NullReferenceException' when I try to read values in my post method from HiddenFor Helpers (Additional information: The object reference is not set for an example of an object. Id 'The name' id 'is not present in the current ID). Why is this and how can I read them? The row in which I get an exception is commented.
As part of my application, I have to implement the questionnaire for topics in which they include general questions and questions. The code for the controller is here:
Public Class Student Controller: Controller {Private EFDbContext Reference; Private string index; Public Student Controller (String Index = "II-2/10") {Reference = New EFDB Contact; This.index = index; } // GET: Student Public View Student Student () {Student Student = Reference. student. Single (I => i.index.Equals (index)); & Lt; Question & gt; Question = Reference questions. Toolist (); Scholarship model = new scholarship (student, question); See Return (Model); } [HTTPS] Public Action Result Student (Försh (Subabzmam Subjects in Model. Shabancs) {Försh (questionnaire question question) {Results Result = New Results (); If (subject.Name == zero) {** // unseen is here! ** Results. Question = Reference questions. Singleor Default (q = & gt; q.id == question.ID). Text; } And {results.Subject = context.Subjects.SingleOrDefault (s = & gt; s.ID == subject.ID) .name; // Databases question question ID is getting the proper question ID (= question.ID - (int) topic id) / (int) math Po (10, topic. Id.ost string (length); result. Question = Reference questions. Singular Lower (q = & gt; q.id == question id). Text; } Results.Answer = context.PossibleAnswers.Single (ans => ans.ID == question selected. Text; Context.Results.Add (results); }} Context.SaveChanges (); See refresh ("full", model); }} See more:
@ Model Questionnaire. Domain. Model StudentWell @ {ViewBag.Title = "Student"; } Using @using (Html.BeginForm ()) {for (int i = 0; i m.Subjects [i]. ID) & lt; H3 & gt; @html DisplayFor (M = & gt; M.jjects [I] .name) & lt; / H3 & gt; // Display the empty string if there is no name (int j = 0; j and lt; model. Shabeks [i] question. Calculation; j ++) {& lt; Div class = "well" & gt; @ Html.HiddenFor (m => m.Subjects [i] .Q [j] .ID) & lt; H3 & gt; @ Html.DisplayFor (m = & gt; m.Subjects [i] .Q [J.]. Text) m.Subjects [i]. Question [h] Selected Ender, answer.ID, new {id = answer.ID}) & lt; Label = "@ answer.ID" & gt; @ Answer.Text & lt; / Labels & gt; & Lt; / Div & gt; } @html. Validation Message (M => M. Subjects [I]. Question [J.] Selected Ender) & lt; / Div & gt; }} & Lt; Input type = "submit" class = "btn btn-success" value = "confirm" /> }
I got a mistake, it was very easy.
if (subject.Name == zero) {** // is unseen here! ** Results. Question = Reference questions. Singleor Default (q = & gt; q.id == question.ID). Text; } There was a problem
subject.Name == blank Because I did not post the topic posted to the method By name to
topic.id == blank Everything is fine.
Comments
Post a Comment