Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net MVC Not Duplicate forms when Edit/Add

When we have anything that requires user input (Eg adding a product to a database) the Edit screen looks the same as the add screen. When using MVC .Net how do you handle this? Do you return the same view? Just adjust the model to reflect the change?

like image 302
LiamB Avatar asked Jun 21 '26 00:06

LiamB


1 Answers

Same (partial)view

You create only one but strong typed view (depending on the UI it can of course be a partial view as well). When adding new data return this view from controller action with default model object instance (usually just a new instance without any properties being set), but when you edit, return it with the object instance that you'd like to edit.

Controller part

Regarding controller actions you can have four of them:

  1. Add GET
    return View("SomeView", new Customer());
  2. Add POST
  3. Edit GET
    return View("SomeView", new CustomerRepository().GetCustomer(id));
  4. Edit POST

Bot GET actions return the same view but with different model as described earlier. POST actions both store submitted data, but return whatever they need to. Probably some RedirectToAction()...

like image 164
Robert Koritnik Avatar answered Jun 22 '26 14:06

Robert Koritnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!