从剃刀视图向通过HTTP Post方法接受值的控制器操作提交值

问题描述 投票:0回答:1

Details的控制器通过post方法接受值,并将复杂的输入对象作为其输入。

这里是使用GET方法发送值导航到详细信息页面的示例方法:

function openDetailsPage(commodityID, commodityName) { dateFrom = convertNumbers2English('@(((FilterViewModel)ViewBag.ViewModel).dateValue_1)'); dateTo = convertNumbers2English('@(((FilterViewModel)ViewBag.ViewModel).dateValue_2)'); dateFrom = changeDateSeparator(dateFrom); dateTo = changeDateSeparator(dateTo); if (dateTo == null || dateTo == undefined) dateTo = "0"; if (dateFrom == null || dateFrom == undefined) dateFrom = "0"; @{ string reportTypes = "0"; if (((FilterViewModel)ViewBag.ViewModel).purchaseReportTypes != null) { reportTypes = String.Join(",", ((FilterViewModel)ViewBag.ViewModel).purchaseReportTypes); } } alert('@reportTypes'); var url = '@Url.Action("ReportDetailed","BuyReport",new { commodityType =(((FilterViewModel)ViewBag.ViewModel).commodityType), commodityName="dummyCommodityName", department=((FilterViewModel)ViewBag.ViewModel).department, repository=((FilterViewModel)ViewBag.ViewModel).repository, commodity ="dummyCommodityID", purchaseReportTypes=reportTypes, dateValue_1="dummyDate1", dateValue_2="dummyDate2" })'; alert(url); @*var url = '@Url.Action("ReportDetailed","BuyReport", new RouteValueDictionary { {"commodityType",((FilterViewModel)ViewBag.ViewModel).commodityType}, {"commodityName","dummyCommodityName" }, {"department",((FilterViewModel)ViewBag.ViewModel).department }, {"repository",((FilterViewModel)ViewBag.ViewModel).repository }, {"commodity","dummyCommodityID"}, {"purchaseReportTypes",((FilterViewModel)ViewBag.ViewModel).purchaseReportTypes }, {"dateValue_1",((FilterViewModel)ViewBag.ViewModel).dateValue_1 }, { "dateValue_2",((FilterViewModel)ViewBag.ViewModel).dateValue_2 } })';*@ url = url.replace("dummyCommodityID", commodityID); url = url.replace("dummyCommodityName", commodityName); url = url.replace("dummyDate1", dateFrom); url = url.replace("dummyDate2", dateTo); alert(url); openLink(url); }

我在使用这种类型的值路由时遇到一些困难:

    输入对象很复杂,所以路由会非常复杂。例如。 /BuyReport/ReportDetailed?commodityType=0&commodityName=dummyCommodityName&department=1&repository=2&commodity=dummyCommodityID&dateValue_1=dummyDate1&dateValue_2=dummyDate2/BuyReport/ReportDetailed/0/itemName/1/2/1/123/
  1. [get参数中的任何特殊字符,例如/都会中断路由
  2. 我无法传递数组之类的东西,所以我应该在发送之前将其转换
  3. 因此,我正在寻找一种使用'Post'方法发送参数的方法,例如表单提交按钮具有以下约束的情况:

    我认为没有表单
  1. 我想将值发布到控制器,页面必须导航到详细信息视图
    1. 第一页中的每个项目都有不同的行和不同的ID,所以我认为为每行创建一个表单是不合理的。
  • 我想知道有什么方法可以根据我的要求实现Post参数?我不在乎是否将C#,JS和jQuery混合使用。

    更多详细信息:

    这是我的列表页面中的示例行,它调用openDetailsPage js函数:

    <a onclick="openDetailsPage(@item.CommodityId,'@Html.DisplayFor(modelItem => item.CommodityName)')"> <div class="ios-content-box px-4 py-1 mb-3 ios-hover-box"> <div class="row font-12 my-2 ios-divider-line"> <div class="col-6 f-w-600 color-orange text-right">@Html.DisplayFor(modelItem => item.CommodityName)</div> <div class="col-6 text-left"> <i class="fas fa-chevron-left fa-fw color-orange "></i></div> </div> <div class="row font-12 my-2 ios-divider-line"> <div class="col-6 text-gray-600 text-right">type</div> <div class="col-6 text-gray-600 text-left">@Html.DisplayFor(modelItem => item.TypesName)</div> </div> <div class="row font-12 my-2 ios-divider-line"> <div class="col-6 text-gray-600 text-right">Code</div> <div class="col-6 text-gray-600 text-left">@Html.DisplayFor(modelItem => item.CommodityCode)</div> </div> <div class="row font-12 my-2 ios-divider-line"> <div class="col-6 text-gray-600 text-right">Barcode</div> <div class="col-6 text-gray-600 text-left">@Html.DisplayFor(modelItem => item.CommodityBarcode)</div> </div> <div class="row font-12 my-2 ios-divider-line"> <div class="col-6 text-gray-600 text-right">Unit Price</div> <div class="col-6 text-gray-600 text-left">@Html.DisplayFor(modelItem => item.UnitPrice)</div> </div> <div class="row font-12 my-2 ios-divider-line"> <div class="col-6 text-gray-600 text-right">Total Price</div> <div class="col-6 text-gray-600 text-left">@Html.DisplayFor(modelItem => item.SumPrice)</div> </div> </div> </a>

    当前我的控制器如下:

    [Route("BuyReport/ReportDetailed/{commodityType}/{commodityName}/{department}/{repository}/{commodity}/{purchaseReportTypes}/{dateValue_1}/{dateValue_2}")] public async Task<ActionResult> ReportDetailed( string commodityType, string commodityName, string department, string repository, string commodity, string purchaseReportTypes, string dateValue_1, string dateValue_2 ) { }

    但是我想将其更改为这样的内容:

    [HttpPost] public async Task<ActionResult> ReportDetailed(DetailedViewModel detailedviewmodel){ string commodity = detailedviewmodel.commodity; string commoditytype = detailedviewmodel.commoditytype; string department = detailedviewmodel.department; string purchasereporttypes = detailedviewmodel.purchasereporttypes; string repository = detailedviewmodel.repository; string startdate = detailedviewmodel.datevalue_1; string enddate = detailedviewmodel.datevalue_2; string commdoityname = detailedviewmodel.commodityname; }

    DetailedViewModel的定义如下:

    public class DetailedViewModel { public string commodityType { get; set; } public string commodityName { get; set; } public string department { get; set; } public string repository { get; set; } public string commodity { get; set; } public string[] purchaseReportTypes { get; set; } public string dateValue_1 { get; set; }//start date public string dateValue_2 { get; set; }//end date }

    我在其中一个具有项目列表的控制器中有一个视图。当用户单击每个项目时,浏览器必须导航到页面,其中包含有关该项目的详细信息。细节控制器接受值...
  • javascript c# asp.net-mvc post razor
    1个回答
    0
    投票
    一种快速简单的解决方案是通过Ajax发布:
    © www.soinside.com 2019 - 2024. All rights reserved.