1

url.action 是:

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

它工作正常,但我不想在 url 中显示 qyery 字符串

网址是:

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`

路线配置是: routes.MapRoute( name: "Home", url: "{controller}/{action}", defaults: new { controller = "Home", action = "Index" } );

控制器中的 ActionResult 是:`

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);
        }`
4

2 回答 2

2

这是描述性解决方案 -

首先你需要有正确的路线 -

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

然后你可以用这种方式定义控制器动作。

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

最后以这种方式链接-

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

生成的 URL -http://localhost:5738/Product/CategoryLevel/1/Rami%20Vemula

当您单击链接时,您将获得如下所示的值 -

在此处输入图像描述

于 2014-02-04T14:06:16.047 回答
0

像这样试试

使用Html.ActionLink代替a

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

或者

<a href='CategoryLevel/@m.ID/@m.Name' >@m.Name</a>
于 2014-02-04T12:42:54.650 回答