1

url.action is :

<li><a href="@Url.Action("CategoryLevel", "Product", new { CategoryId = @item._categoryId, ProductName = @Html.Raw(item._categoryName) })">@Html.Raw(item._categoryName)</a></li>

it works fine but i dont want to display the qyery string in url

url is:

http://localhost:99/Product/CategoryLevel?CategoryId=16&ProductName=Common%20Conditions

i want to display this as


`http://localhost:99/Product/CategoryLevel/16/Common%20Conditions`  (or)`http://localhost:99/Product/CategoryLevel/Common%20Conditions(or)http://localhost:99/Product/Common%20Conditions`

route config is: routes.MapRoute( name: "Home", url: "{controller}/{action}", defaults: new { controller = "Home", action = "Index" } );

ActionResult in controller is:`

public ActionResult CategoryLevel()
        {
            string ProductName = Request.QueryString["ProductName"];
            ViewBag.ProductName = ProductName;
            int Category = Convert.ToInt32(Request.QueryString["CategoryId"]);
            ViewBag.ParentCategoryId = Category;
            int ParentCategoryId = 0;
            if (Request.QueryString["ParentCategoryId"] != null)
            {
                ParentCategoryId = Convert.ToInt32(Request.QueryString["ParentCategoryId"]);
            }
            Product productInstance = new Product();
            IList<CategoryInfo> categories = new List<CategoryInfo>();
            categories = productInstance.GetCategories(Category, true);
            if (categories.Count == 0)
            {
                return RedirectToAction("NewProducts", "Product", new { @CategoryId = Category,  ProductName = ProductName });
            }

            return View(categories);

        }`another actionresult is`public ActionResult NewProducts(Product instance)
        {
            string ProductName = Request.QueryString["ProductName"];
            instance.BrandName = ProductName;
            int CategoryId = Convert.ToInt32(Request.QueryString["CategoryId"]);
            int BrandId = Convert.ToInt32(Request.QueryString["BrandId"]);
            string SortBy = Convert.ToString(Request.QueryString["sortBy"]);
            if (SortBy != null)
            {
                Session["Sort"] = SortBy;
            }
            Session["NewProductsBrandId"] = BrandId;
            instance.CategoryId = CategoryId;
            instance.BrandId = BrandId;
            instance.SortBy = SortBy;
            return View(instance);
        }`
2

2 Answers 2

2

Here goes descriptive solution -

First you need to have proper route -

routes.MapRoute(
    name: "ProductDetails",
    url: "{controller}/{action}/{categoryid}/{productname}",
    defaults: new { controller = "Product", action = "CategoryLevel" }
);

Then you can have controller action defined in this way.

public class ProductController : Controller
{
    public ActionResult CategoryLevel(string CategoryId, string ProductName)
    {
        return View();
    }
}

Finally the link in this way -

@Html.ActionLink("sample","CategoryLevel", "Product", new { CategoryId = 1, ProductName = "Rami Vemula" }, null)

URL generated - http://localhost:5738/Product/CategoryLevel/1/Rami%20Vemula

And when you click on link, you will get values as shown below -

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

url is rewrited but it will not redirect to the next page
0

Try like this,

Use Html.ActionLink instead of a

 @Html.ActionLink("EditUser", "CategoryLevel", "Product", new { Id = m.ID }, new { @class = "hide" })

Or

<a href='CategoryLevel/@m.ID/@m.Name' >@m.Name</a>

5 Comments

it's also generates the link with query string right ?
@SivaRajini No it generate query like this http://localhost:53897/Test/DYmanicControllerPage/2
it generates the url as expects but it will not redirect to next page@Jaimin
Common%20Conditions how can i display this as Common Conditions (or) Common+Conditions(or) Common-Conditions @ jaimon
@user3166404 i don't understand your comment.please elaborate.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.