[Asp.net MVC5]使用httppost通过actionlink传递对象 [英] [Asp.net MVC5] passing objects with the actionlink, using httppost

查看:269
本文介绍了[Asp.net MVC5]使用httppost通过actionlink传递对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!我将对象(隐藏)发送到控制器以使其与它们一起操作时遇到问题.
我有一个表,在其第一列的值中具有超链接.
我想调用一个获取该行对象的方法,但是我不希望该用户看到该对象.我该如何实现?

我知道不可能发送对象,因此我使用了我发现的使用json的代码.

我尝试过的事情:

这是我的代码...

风景,风景

Hello! I have a problem with sending objects (as hidden) to the controller for it to operate with them.
I have a table that has hyperlinks in its first column''s values.
I want to call a method which recives the object of that row, but I dont want that user to see the object. How can I achieve this?

I know it is not possible to send an object, so I used a code that I found , using json.

What I have tried:

Here is my code...

Firts, the View

<div class="form-group">
           <table class="table">
               <tr>
                   <th>Cbte</th>
                   <th>Total</th>
                   <th>Fecha</th>
               </tr>

               @foreach (var item in Model.Comprobantes) {
               <tr>
                   <td> HERE!!!
                       @Html.ActionLink(item.Comprobante, "Descargar", "Comprobantes", new { JSONModel = Json.Encode(item) } , null)
           </td>
                   <td>
                       @Html.DisplayFor(modelItem => item.Total)
                   </td>
                   <td>
                       @Html.DisplayFor(modelItem => item.Fecha)
                   </td>
               </tr>
               }
           </table>
       </div>



控制器



The controller

[HttpPost]
        public ActionResult Descargar(string JSONModel)
        {
	   //nothing here yet
            return View();
        }


如果我离开,这不起作用[HttpPost]
错误:说明:HTTP404.您正在寻找的资源(或其依赖项之一)可能已被删除,名称更改或暂时不可用.
请查看以下URL,并确保其拼写正确.
如果我将其删除,则可以正常工作,但用户会在路由路径中看到所有数据,而这并不是主意.
https://k60.kn3.net/1/6/B/0/C/B/CB3.png
这是我的视图模型
列表包含我在表中显示的值


This doesn''t work if I leave[HttpPost]
Error: Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.
Please review the following URL and make sure that it is spelled correctly.
If I remove it, does work but the user see in the route path all the data and it''s not the idea.
https://k60.kn3.net/1/6/B/0/C/B/CB3.png
And this is my viewmodel
The list contains the values that I show in the table

public class ComprobanteViewModels
    {
        [DataType(DataType.DateTime)]
        [Display(Name = "Fecha desde: ")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        public DateTime? FechaDesde { get; set; }

	[DataType(DataType.DateTime)]
        [Display(Name = "Fecha hasta: ")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        public DateTime? FechaHasta { get; set; }
       
        [DataType(DataType.Text)]
        [Display(Name = "Comprobantes")]
        public List<ComprobanteData> Comprobantes { get; set; }
    }

推荐答案



我认为您可以序列化和反序列化对象以获得所需的输出.

使用
将用户定义的对象转换为javascript选项
Hi,

I think you can serialize and de-serialize the object to get the desired output.

convert an user-defined object to the javascript option using

Json.Enocode(Item)



然后使用ajax函数调用将该对象传递给控制器​​.

< script src =〜/Scripts/jquery-1.10.2.min.js"></script>
< script>
函数commitForm(item){

var obj = Json.Enocode(item);



Then pass the object to the controller using the ajax function call.

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
function submitForm(item) {

var obj= Json.Enocode(item);

jQuery.ajax({
          type: "POST",
          url: "@Url.Action("ActionName with controller")",
          dataType: "json",
          contentType: "application/json; charset=utf-8",
          data: JSON.stringify(obj),
          success: function (data) { alert(data); },
          failure: function (errMsg) {
              alert(errMsg);
          }
      });


}
</script>

只需在控制器中反序列化结果即可.


}
</script>

just de-serialize the result in your controller.

public ActionResult Descargar(string JSONModel)
        {
	   var data = JsonConvert.DeserializeObject<User-DefinedClassName>(jsonString);

            return View();
        }




参考:
[
[^ ]




Reference:
[^]


[^]


这篇关于[Asp.net MVC5]使用httppost通过actionlink传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆