0

Scenario is Have a GET method (Create) in which we are getting a collection into ViewBag and passing it to View to fill dropdown. Now when the ajax begin form post (Create post) happens, returning same view which requires same collection which we filled in GET (Create) method. Can we avoid this refilling of collection by any alternative way or reuse filled collection in GET method; as ultimately returning same view?

Sample code is -

  // GET: Departments/Create
    public ActionResult Create()
    {
        ViewBag.RoleId = new SelectList(db.Role, "Id", "Name");
        ViewBag.UserId = new SelectList(db.User, "Id", "Name");
        return PartialView();
    }
 // POST: Departments/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,Name,RoleId")] Department department)
    {
        if (ModelState.IsValid)
        {
            db.Department.Add(department);
            db.SaveChanges();
            return PartialView(department);
        }

        ViewBag.RoleId = new SelectList(db.Role, "Id", "Name", department.RoleId);
        ViewBag.UserId = new SelectList(db.User, "Id", "Name");
        return PartialView(department);
    }

Aim is to avoid these two lines in post

 ViewBag.RoleId = new SelectList(db.Role, "Id", "Name", department.RoleId);
 ViewBag.UserId = new SelectList(db.User, "Id", "Name");

as it could add overhead in performance once the data increased.

Can you please guide on this?

Oxygen
  • 831
  • 4
  • 17
  • 42
  • 1
    You must repopulate the SelectLists as you did in the GET method. But if you making an ajax call, what is the point of returning exactly the same view again - you already have it. –  Feb 05 '18 at 12:34
  • All you need to to return a `JsonResult` indicating success or otherwise (although its not clear why you would want to stay on the same page anyway after the user has edited and saved the record anyway) –  Feb 05 '18 at 12:36
  • You mean, even if I post view using Ajax.Beginform , collection which bound to dropdown won't lost? – Oxygen Feb 05 '18 at 12:36
  • @StephenMuecke. yes wehave the scenario that user can edit after saved the information hence returning same view. – Oxygen Feb 05 '18 at 12:37
  • Therefore, just return a `JsonResult` indicating success or otherwise (there is no point returning the same html you already have in the view) –  Feb 05 '18 at 12:42
  • Its also clear you have other problems with your code such as [Can the ViewBag name be the same as the Model property name in a DropDownList?](https://stackoverflow.com/questions/37161202/can-the-viewbag-name-be-the-same-as-the-model-property-name-in-a-dropdownlist). Your editing data so always use a view model, and that view model will contain `IEnumerable` properties for use in your `DropDownListFor()` methods –  Feb 05 '18 at 12:46
  • In your POST action, perform all operations and return JsonResult indicating success/ failure. No need to populate ViewBag in this case, because as it is an Ajax result, your dropdowns still would be having the same list, unless you have updated them within "success" event of Ajax call. – Nirman Feb 05 '18 at 13:26

0 Answers0