-1

On my MVC webpage I have an ActionLink like this:

        @Html.ActionLink("Create", "Index", "Project")

The view is Index.cshtml and the controller is "Project".

When clicked on this link I want it to go to the controller and do actionResult for Create. But when I click it it wants to go to /projects/index and nothing happens because the actionresult method for create isn't called. It calls the actionresult for Index. So nothing actually happens, the page simply refreshes.

If I change the Actionlink to:

@Html.ActionLink("Create", "Create", "Project")

it tries to go to "/projects/create" but can't find it because it doesn't exist.

My ActionResult code looks like this:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "projectName,projectType")] Project project)
{
  if (ModelState.IsValid)
  {
    db.Project.Add(project);
    db.SaveChanges();
    return RedirectToAction("Index");
  }

  return View(project);
}

How do I solve this?

zed
  • 2,298
  • 4
  • 27
  • 44
Eric
  • 339
  • 3
  • 14
  • Url is `/projects/index` and you are using `Project` show your Project controller – Satpal Dec 01 '15 at 12:14
  • So you have controller named: ProjectController with action method Create, and also a model named Project? – MaticDiba Dec 01 '15 at 12:24
  • yes, although VS2015 adds an s to the name so it is called ProjectsController. Don't why VS puts the s in there. – Eric Dec 01 '15 at 12:26
  • Show the GET method for `Create()` - all you have shown is the POST method which will not be called by a link (which is a GET) –  Dec 01 '15 at 12:27
  • You have defined the Action for `POST` request which accepts `Project` object where as ActionLink will generate link which will be a `GET` request – Satpal Dec 01 '15 at 12:27
  • I do not have a get method for Create because it is all on the same page. On the Index page I have a section where the user fills in the project name and selects a type from list. Then he presses a button (the actionlink) and it should be put in the database (the action result). But how do I get it there? – Eric Dec 01 '15 at 12:33
  • Of course you need a GET method if you have a link. If your modifying data in a database, the you need a form and a submit button to POST the form values associated with `Project` –  Dec 01 '15 at 12:55

3 Answers3

0

First parameter is text that you want to display, second is action, third is controller.

The right way to use it it would be:

@Html.ActionLink("Some text", "Create", "Project")
MaticDiba
  • 895
  • 1
  • 11
  • 19
  • Did you create your action like: public ActionResult Create() { return view()} in your Project controller. – MaticDiba Dec 01 '15 at 12:19
  • In the case you specified above, you defined an ActionResult method, that responds to Post method. With actionlink you don't create a post call. Check: http://stackoverflow.com/questions/2048778/asp-net-mvc-actionlink-and-post-method – MaticDiba Dec 01 '15 at 12:26
  • I copied that from the default code Visual studio generates when you create a MVC website. – Eric Dec 01 '15 at 12:28
  • I'm also getting the extra "S" on the controller name. It also happens with Html.ActionLink AND Html.BeginForm – Joseph Norris Feb 14 '19 at 16:15
0

Ok, got it to submit to the right actionResult.

What I did was putting the section with the input inside

@using (Html.BeginForm("Create","Projects"))
{
  @Html.AntiForgeryToken()
  // my html code here
  // submit button here
}

and I replaced the actionLink with a submit input html.

But now I do get in the right ActionResult method, still there is no data. I'll create a new question for that.

thanks for all the help!!

Eric
  • 339
  • 3
  • 14
0

Check the App_Start\RouteConfig.cs

Change

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

TO

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "home", action = "Index", id = UrlParameter.Optional }
        );
Joseph Norris
  • 368
  • 1
  • 11