0

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 newbie in ASP.NET 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.

Please suggest me good solution.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Muhammad Atif Agha
  • 1,535
  • 3
  • 33
  • 74

2 Answers2

2

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.

Community
  • 1
  • 1
Yoda
  • 17,363
  • 67
  • 204
  • 344
0
local-host:1613/Patient/Index/1

This path should be hitting your action method of:

 public ActionResult Index(int id)

Please check your RouteConfig.cs file and make sure you have the default route set up correctly:

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

Try commenting out your other Index action method "//public ActionResult Index()", and see what happens when trying to go to local-host:1613/Patient/Index/1

kd_
  • 66
  • 3