发布表单将路由值作为查询字符串而不是网址段发送

时间:2019-01-06 18:36:53

标签: c# asp.net-mvc

使用@ Html.BeginForm发布表单时,生成的url中的路由值将以查询字符串而不是url段的形式发送,从而导致路由错误。

控制器:

public static void Main(string[] args)
        {
            DateTime timeArrived = new DateTime(2019, 1, 5, 13, 53, 0);
            DateTime timeExit = new DateTime(2019, 1, 6, 8, 46, 0);

            TimeSpan startMeal1 = new TimeSpan(6, 0, 0);
            TimeSpan endMeal1 = new TimeSpan(8, 0, 0);
            TimeSpan startMeal2 = new TimeSpan(12, 0, 0);
            TimeSpan endMeal2 = new TimeSpan(14, 0, 0);
            TimeSpan startMeal3 = new TimeSpan(17, 0, 0);
            TimeSpan endMeal3 = new TimeSpan(19, 0, 0);


            int daysDiff = (timeExit - timeArrived).Days;

            int meals1Count = daysDiff;
            int meals2Count = daysDiff;
            int meals3Count = daysDiff;

            TimeSpan timeDiff = timeExit - timeArrived - TimeSpan.FromDays(daysDiff);

            if (timeArrived.TimeOfDay <= endMeal1 && (timeArrived.TimeOfDay + timeDiff) >= startMeal1) meals1Count++;
            if (timeArrived.TimeOfDay <= endMeal2 && (timeArrived.TimeOfDay + timeDiff) >= startMeal2) meals2Count++;
            if (timeArrived.TimeOfDay <= endMeal3 && (timeArrived.TimeOfDay + timeDiff) >= startMeal3) meals3Count++;
        }

剃刀(cshtml):

[HttpPost]
[Route("Add-Supplier/{id}/{supplierId}")]
[ActionName("Add-Supplier")]
public ActionResult AddSupplier(int id, int supplierId)
{
    rmPurchaseOrder purchaseOrder = rmPurchaseOrder.Load(id);
    if (purchaseOrder != null)
    {
        purchaseOrder.SupplierId = supplierId;
        purchaseOrder.Save();
    }
    return RedirectToAction("Purchase-Order", new { id });
}

表单发布的带有以下代码的网址是:

/ Add-Supplier / 3777?supplierId = 25

我希望它是:

/ Add-Supplier / 3777/25

任何帮助表示感谢,谢谢...

1 个答案:

答案 0 :(得分:0)

您可以尝试以下步骤:

第1步:在配置文件中写入路由。

routes.MapRoute(
  name: "Segment",
  url: "Add-Supplier/{id}/{id1}",
  defaults: new 
  { 
    controller = "Your controller Name", 
    action = "Your Action Name", 
    id = UrlParameter.Optional, 
    id1 = UrlParameter.Optional
  }
);

第2步:用以下方法替换您的操作方法。

public ActionResult AddSupplier(int id, int id1)
{

}