0
votes

I have created controller for add comment on web page. below is my comment controller

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

this is AddComment View

@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>
}

below is comment model

[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 have got error message

'The parameters dictionary contains a null entry for parameter 'spid' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult AddComment(WEB1.Models.comment, Int32)' in 'WEB1.Controllers.CommentController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters'.

I have debugged my code it doesn't hit on post Action method. How do I fix this problem.

2
I have modified my Question - OK91
@StephenMuecke thanks a lot.It works - OK91

2 Answers

0
votes

See the error message, it's clearly saying what's wrong. Your post method models are not binded properly. parameter spid is null and so it's failing while validating model binding, particularly in the line if (ModelState.IsValid) {

Your error message

a null entry for parameter 'spid' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult AddComment(WEB1.Models.comment

0
votes

You do not pass a model to the view so the value of property SPID is null in the view (your @Html.HiddenFor(model => model.SPID) is generating <input type="hidden" name="SPID" .... value="" />) so a null value is posted when you submit the form.

Change your GET method to initialize the model, set its property, and then return that model to the view

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

and you can also remove the int spid parameter in your POST method since the value is already bound to the SPID property of cmt

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