-3

I have a page where i am displaying image, id, name and address. I have added checkbox and multiple submit buttons to delete update or add new records.
But when I click on any submit buttons i am getting unhandled exception error. (for example I have selected a checkbox and clicked delete button my control is going to Mycontroller from view and to the delete switch case. It is deleting the record but again when it is coming back to view I am getting the NullReferenceException.)

View :

@using (Html.BeginForm("MyAction", "MyController", FormMethod.Post))

{
   @if (Model.Count> 0) // .NullReferenceException
   {
      @foreach (var MyDB in Model)
      {              
         <div class="col-md-2">
              <img src="@Url.Content(photos.photo_url)" style="width:100px; height:100px;" />
          </div>

           Html.LabelFor(model => MyDB.ID, new
           {
               @class = "control-label col-md-2"
           })
           <div class="col-md-2">
               @Html.EditorFor(model => MyDB.ID)
               @Html.ValidationMessageFor(model => MyDB.ID)
            </div>
             @Html.LabelFor(model => MyDB.name, new
             {
                 @class = "control-label col-md-2"
             })
             <div class="col-md-2">
                  @Html.EditorFor(model => MyDB.name)
                  @Html.ValidationMessageFor(model => MyDB.name)
             </div>
             @Html.LabelFor(model => MyDB.address, new
             {
                 @class = "control-label col-md-2"
             })
             <div class="col-md-2">
                  @Html.EditorFor(model => MyDB.address)
                  @Html.ValidationMessageFor(model => MyDB.address)
             </div>
              <div class="col-md-1">
                    input type="checkbox" class="checkboxes" value="@MyDB.id" name="id" />
              </div>                          
        }
}
}
 <div class="col-md-offset-2 col-md-10">

       <input type="submit" name="submitButton" value="UPDATE" />
        <input type="submit" name="submitButton" value="ADD" />
        <input type="submit" name="submitButton" value="DELETE" />
</div>

Controller :

public ActionResult MyController(IEnumerable<int> id, string submitButton)
        {

            switch (submitButton)
            {
                case "DELETE":
                   foreach (var item in id)
                    {
                        var delete = _db.MyDB.FirstOrDefault(s => s.id == item);
                        if (delete != null)
                        {
                            _db.MyDB.Remove(delete);
                        }
                    }

                break;

                case "ADD":
                    if (!ModelState.IsValid)
                    {
                        return RedirectToAction("ADDRecordPage", new
                        {
                            id
                        });
                    }

                    if (id == null)
                    {
                        return RedirectToAction("ADDRecordPage", new
                        {
                            id
                        });
                    }
                break;
                case "UPDATE" :
                ViewBag.Message = "UPDATE";
                break;
            }

            _db.SaveChanges();

            ViewBag.Message = "Saved";
            return View();
        }
Nav
  • 71
  • 13
  • 3
    What unhandled exception are you getting? Where is it happening? – ivcubr Jul 06 '18 at 19:03
  • 1
    I downvoted because there is large amount of information lacking to answer this question. I highly recommend reading [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – Erik Philips Jul 06 '18 at 19:07
  • It's impossible to say how you can improve your code without the exception details. – Dan Wilson Jul 06 '18 at 19:09
  • getting NullReferenceException at @if (Model.Count> 0) – Nav Jul 06 '18 at 19:22
  • Make sure that you have the proper model definition at the top of your view. Looks like it should be something similar to `@model List` If you have that make sure you are returning the correct model to your view – ivcubr Jul 06 '18 at 19:24
  • Yes I am passing the right model definition. But still I get NullReferenceException. Is anything else am i missing? – Nav Jul 06 '18 at 19:36
  • Can put post the controller code that is returning the model to the view? Also when you debug, at the end of this method what is the state of the model? – ivcubr Jul 06 '18 at 19:39
  • Here is my controller code returing model to view public ActionResult MyController(int id) { var result = _db.MyDB.Where(p => p.id == id).ToList(); return View(result); } MyController is returning null to view – Nav Jul 06 '18 at 20:12
  • I found out what mistake I did. Inside controller i added return View(); it was returning null. so changed to return RedirectToAction("MyAction", new { id }); instead of return View();. – Nav Jul 06 '18 at 21:05

1 Answers1

0

I found out what mistake I did. Inside controller i added return View(); it was returning null. so changed to return RedirectToAction("MyAction", new { id }); instead of return View();

Nav
  • 71
  • 13
  • You have far more problems that just that! All your `EditorFor()` are completely pointless and are not even posted back anyway - you do not even have a parameter in your method for it, but in any case you cannot use a `foreach` loop to generate form controls for a collection (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943)) –  Jul 06 '18 at 22:52