0

i have the following view

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using( Html.BeginForm("Create", "Concepts", new  { name="sfsfsfsfsf", gio="sfsf9s9f0sffsdffs", ford="mtp"}, FormMethod.Get, null ) )
{
    <input type="submit" name="name" value="New" />
}

when i click the new button how do i show the values gio, ford and name on the URL?

this is my route definition

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
7
  • Your adding route values, not query string values. You need to define a specific route, or add hidden inputs. Show your route definitions and the signature of the Create method (and you also need to remove name="name" from the button)
    – user3559349
    Feb 13, 2016 at 1:02
  • @StephenMuecke you got me there :) what are route definitions and their purpose
    – zeelong
    Feb 13, 2016 at 1:20
  • Add the code as per my first comment and I'll add an answer explaining what you have to do (your existing route definitions are defined in the RouteConfig.cs file)
    – user3559349
    Feb 13, 2016 at 1:24
  • @StephenMuecke i have the default URL setup this is for the MVC route address but how do i create a route for q regular query string
    – zeelong
    Feb 13, 2016 at 1:30
  • @StephenMuecke i want to drag this further assume i have a route address like localhost:17765/Product/GetAllProductsByCatId/1 when this was called display a products in a category it will pefrectly add all boostrap and displays things nicely but if i call the same product using query string localhost:17765/Product?GetAllProductsByCatId=1 this display the page but it has no bootstrap, why is this
    – zeelong
    Feb 13, 2016 at 1:45

1 Answer 1

1

You usage of BeginForm() is adding 3 route values, not query string values. If you want to generate a url which is .../Concepts/Create/sfsfsfsfsf/sfsf9s9f0sffsdffs/mtp which would go to (in ConceptsController)

public ActionResult Create(string name, string gio, string ford)

Then you need to add the following route definition (and it needs to be before the Default route

routes.MapRoute(
    name: "Create",
    url: "Concepts/Create/{name}/{gio}/{ford}",
    defaults: new { controller = "Concepts", action = "Create" }
);

Note also that you need to remove name="name" from your submit button because of the conflict with the route parameter

Alternatively, if you want .../Concepts/Create?name=sfsfsfsfsf@&gio=sfsf9s9f0sffsdffs&ford=mtp, then remove the route parameters and add inputs for the values

@using( Html.BeginForm("Create", "Concepts", FormMethod.Get) )
{
    <input name="name" value="sfsfsfsfsf" />
    <input name="gio" value="sfsf9s9f0sffsdffs" />
    <input name="ford" value="mtp" />
    <input type="submit" value="New" />
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.