0

I want to edit my data in ASP MVC in entity model. However, when the form is sent, my controller receive null values(error in the controller). I don't know why it happened. I am new in MVC.

System.NullReferenceException: 'Object reference not set to an instance of an 
object.'
getmenu was null.

I don't understand why it has null values. Can anyone elaborate?

model

namespace Shop
{
    using System;
    using System.Collections.Generic;

    public partial class menu
    {
        public int id { get; set; }
        public string name { get; set; }
        public string type { get; set; }
        public Nullable<double> price { get; set; }
    }
    }

view

@model Shop.menu

@{
    ViewBag.Title = "edit";
}

<h2>edit</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>menu</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.id)

        <div class="form-group">
            @Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.type, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.type, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.type, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.price, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.price, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.price, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Controller

namespace Shop.Controllers
{
    public class HomeController : Controller
    {
        shopcontext db = new shopcontext();

 public ActionResult edit()
        {

            return View();
        }

        [HttpPost]
        public ActionResult edit(menu menu)
        {

            var getmenu = db.menus.Where(p => p.id == menu.id).FirstOrDefault();
            getmenu.name = menu.name;
            getmenu.type = menu.type;
            getmenu.price = menu.price;
            db.SaveChanges();
            return RedirectToAction("Index");

        }
}
}

those are all the code. please let me know your thoughts!

Raspi Surya
  • 315
  • 2
  • 11
  • 28

1 Answers1

0
  1. Replace

    @using (Html.BeginForm())

    by

    @using (Html.BeginForm("edit", "Home", FormMethod.Post))

2.

[HttpGet]
public ActionResult edit()
{
   var model= new Shop.menu();
    return View(model);
}

3.

[HttpPost]
 [ValidateAntiForgeryToken]
public ActionResult edit(menu menu)
{
    //........
}
Ashiquzzaman
  • 5,129
  • 3
  • 27
  • 38