Fill a form with ASP MVC Querystring

Total Post:39

ASP.Net  Mvc 
 1438  View(s)
Rate this:
I want to fill a form with Id and i have made the url in this way


Where as 1 is the Id. And i want to fill my form with the data of patient with Id 1. But this below function is not being called in Controller

 public ActionResult Index(int id)
            Patient p = new CEntities().Patients.Find(id);

            return View(p);
//instead, i am getting into
 public ActionResult Index()
            return View();
I am new bie in ASP MVC, i have no idea in filling a form with any other way, i found a way but it shows my whole object in query string which is insecure. And this above solution is not working.
  1. Post:41

    Re: Fill a form with ASP MVC Querystring

    As far as I know you cannot truly overload action methods in ASP.NET-MVC so there can be only one Index action method(without any additional annotations). Of course you can define few methods with the same name but then you might want to add  [HttpPost](or other HTTP method) annotation above the action method to use it for instance after submitting a form.

    Making parameter optional might be helpful:

    public ActionResult Index(int? id){
                Patient p = new CEntities().Patients.Find(id);
                return View(p);
       return View();
    Please take a look: Can you overload controller methods in ASP.Net MVC?
    Another advice: try to use scaffolding and see how template does it for you:

    1.Right click on a controller folder

    2.Add->New Controller

    3.Controller with Entity Framework read/write actions(as far as I remember 3rd from the top)

    4.Select model class.

    5.Select db context(class which maintains connection with database).

    It will generate controller with views, have fun.

      Modified On Apr-07-2018 05:50:35 AM