2016-06-26 3 views
0

Ich habe Controller für den Kommentar auf der Webseite erstellt. unten ist mein Kommentar ControllerFormular nicht senden Fehler kommt als Nulleintrag für Parameter in Mvc 4

public ActionResult AddComment(int id=0) 
{ 
    int spid = id; 
    return View(); 
} 

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult AddComment(comment cmt,int spid) 
{ 
    if (ModelState.IsValid) 
    { 
     cmt.SPID = spid; 
     db.comments.Add(cmt); 
     db.SaveChanges(); 
     return RedirectToAction("Index", "Comment"); 
    } 
    return View(cmt); 
} 

diese AddComment Ansicht

@model WEB1.Models.comment 
@using (Html.BeginForm("AddComment", "Comment")) 
{ 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>comment</legend> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.cmd_content) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.cmd_content) 
      @Html.ValidationMessageFor(model => model.cmd_content) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.t_email) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.t_email) 
      @Html.ValidationMessageFor(model => model.t_email) 
     </div> 
      @Html.HiddenFor(model => model.SPID) 
     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 

unten ist

Kommentar Modell
[Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int CMT_ID { get; set; } 

    private DateTime _date = DateTime.Now; 
    public DateTime cmd_ad 
    { 
     get { return _date; } 
     set { _date = value; } 
    } 
    [Required(ErrorMessage = "Please add Comment before submit")] 
    public string cmd_content { get; set; } 
    [RegularExpression(@"^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$", 
    ErrorMessage = "Please Enter Correct Email Address")] 
    public string t_email { get; set; } 
    public Nullable<int> SPID { get; set; } 

I Fehlermeldung

‚Die Parameter Wörterbuch enthält bekommen haben ein Nulleintrag für den Parameter 'spid' von Nicht-Nu lable Typ 'System.Int32' für Methode 'System.Web.Mvc.ActionResult AddComment (WEB1.Models.comment, Int32)' in 'WEB1.Controllers.CommentController'. Ein optionaler Parameter muss ein Referenztyp, ein Nullable-Typ oder ein optionaler Parameter sein. Parametername: Parameter '.

Ich habe meinen Code debuggte es nicht auf Post Action-Methode. Wie behebe ich dieses Problem?

+0

ich habe meine Frage – OK91

+0

@StephenMuecke dank ein lot.It Werke geändert – OK91

Antwort

0

Sie haben nicht ein Modell zur Ansicht übergeben, so wird der Wert der Immobilie SPIDnull in der Ansicht ist (Ihre @Html.HiddenFor(model => model.SPID) generiert <input type="hidden" name="SPID" .... value="" />), so wird ein null Wert beim Absenden des Formulars veröffentlicht.

Ihre GET-Methode ändern Sie das Modell, setzen Sie seine Eigenschaft, zu initialisieren und dann dieses Modell zurück zur Ansicht

public ActionResult AddComment(int id = 0) 
{ 
    var model = new comment(){ SPID = id }; 
    return View(model); 
} 

und Sie können die int spid Parameter in der POST-Methode auch entfernen, da der Wert bereits gebunden ist zum SPID Eigenschaft cmt

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult AddComment(comment cmt) 
{ 
    if (ModelState.IsValid) 
    { 
     db.comments.Add(cmt); 
     db.SaveChanges(); 
     return RedirectToAction("Index", "Comment"); 
    } 
    return View(cmt); 
} 
0

Siehe die Fehlermeldung, es wird deutlich gesagt, was falsch ist. Ihre Post-Methoden-Modelle sind nicht richtig verknüpft. spid Parameter ist null und es ist so versagt, während Modell Validierung Bindung, vor allem in der Linie if (ModelState.IsValid) {

Ihre Fehlermeldung

ein Null-Eintrag für den Parameter 'spid' von Nicht-Nullable Types 'System.Int32' bei der Methode ‚System.Web.Mvc.ActionResult AddComment (WEB1.Models.comment

Verwandte Themen