Home > DeveloperSection > Forums > Fill a form with ASP MVC Querystring
Andrew Watson
Andrew Watson

Total Post:39

Points:273
Posted on    December-26-2014 10:07 PM

 .NET ASP.Net  Mvc 
Ratings:


 1 Reply(s)
 645  View(s)
Rate this:
I want to fill a form with Id and i have made the url in this way

local-host:1613/Patient/Index/1

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.


Monaj Singh
Monaj Singh

Total Post:41

Points:287
Posted on    December-26-2014 10:09 PM

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){
        if(id.HasValue()){
            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).

6.Ok.

It will generate controller with views, have fun.

Modified On Apr-04-2016 07:40:04 AM

Don't want to miss updates? Please click the below button!

Follow MindStick