简体   繁体   English

如何通过传递对象作为Asp.Net MVC 4中的参数来执行动作链接?

[英]How to execute an action link by passing an object as parameter in Asp.Net MVC 4?

I would like to execute an action by passing an object as a parameter. 我想通过传递一个对象作为参数来执行一个动作。 To do that I have serialized that object to a Json string then converted that string into Hex string, passed that Hex string as parameter in the action link. 为此,我已将该对象序列化为Json字符串,然后将该字符串转换为Hex字符串,并将该Hex字符串作为参数传递给action链接。

I have an object like this, 我有一个这样的东西,

Product prodObject = new Product { Categories = new Category { CategoryName = "Stationary" }, ProductName = "Book", Price = 250 };

Serialized above object to Json string like this, 这样将上面的对象序列化为Json字符串,

var jsontString = JsonConvert.SerializeObject(prodObject);

I have converted above Json String into Hex string using below code. 我已经使用以下代码将上面的Json String转换为Hex字符串。

public string ConvertStringtoHex(string encodeMe, System.Text.Encoding encoding)
    {
        Byte[] stringBytes = encoding.GetBytes(encodeMe);
        StringBuilder sbBytes = new StringBuilder(stringBytes.Length * 2);
        foreach (byte b in stringBytes)
        {
            sbBytes.AppendFormat("{0:X2}", b);
        }
        return sbBytes.ToString();
    }

In my action result return like below, 在我的动作结果返回如下

string hexString = ConvertStringtoHex(jsontString, System.Text.Encoding.Unicode);
return JavaScript(string.Format("document.location = '{0}';", Url.Action("Checkout", "PurchaseModule", new { pid= hexString })));

Again I have converted that Hex string to Json string at the ActionResult Post method then I tried to convert Json string to my original object. 再次在ActionResult Post方法中将十六进制字符串转换为Json字符串,然后尝试将Json字符串转换为原始对象。

string MyString = ConvertHextoString(pid, System.Text.Encoding.Unicode);

Converted Hex string into Json string using below code, 使用以下代码将十六进制字符串转换为Json字符串,

public static string ConvertHextoString(string decodeMe, System.Text.Encoding encoding)
    {
        int numberChars = decodeMe.Length;
        byte[] bytes = new byte[numberChars / 2];
        for (int i = 0; i < numberChars; i += 2)
        {
            bytes[i / 2] = Convert.ToByte(decodeMe.Substring(i, 2), 16);
        }
        return encoding.GetString(bytes);
    }

De serialized string to Json String like below, 如下所示将序列化的字符串反序列化为Json String,

var JsonString = JsonConvert.DeserializeObject(MyString);

I am getting error at the below line code, 我在下面的代码行中遇到错误,

Product prodObject = (Product)JsonString;

Here I am getting an error like below. 在这里,我收到如下错误。 Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Product'.

Does anyone know how to solve this issue? 有谁知道如何解决这个问题?

尝试这个:

Product prodObject = JsonConvert.DeserializeObject<Product>(MyString);

Adding comment as answer: 添加评论作为答案:

Deserialise directly into the correct type: 直接反序列化为正确的类型:

var prodObject = JsonConvert.DeserializeObject<Product>(MyString);

如何获取列表<object>作为 ASP.NET Core MVC 中的参数<div id="text_translate"><p>我有一个 class,它的名字是“Question.cs”,我还有一个名字是“Answer.cs”。 我想向我的操作发送问题列表而不是一个问题,但我不知道该怎么做。 事实上,我不知道如何将问题作为参数。 谁能告诉我如何在行动中收到问题? 这是我的课程:</p><pre> public class Question { [Key] public int QuestionId { get; set; } [Required] [MaxLength(500)] public string QuestionTitle { get; set; } [Required] [MaxLength(500)] public string QuestionAnswer { get; set; } //property public List&lt;Answer&gt; Answers { get; set; } }</pre><pre> public class Answer { [Key] public int AnswerId { get; set; } [Required] public int QuestionId { get; set; } [Required] [MaxLength(500)] public string AnswerTitle { get; set; } //property [ForeignKey("QuestionId")] public Question Question { get; set; } }</pre><p> 这是我的 Razor 视图(Question.cshtml):</p><pre> @using GameShop.Data.Domain.QuestIonsAnswers @model List&lt;GameShop.Data.Domain.QuestIonsAnswers.Question&gt; @{ Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml"; } &lt;form asp-action="Index" method="POST"&gt; @foreach (var item in Model) { &lt;div&gt;@item.QuestionTitle&lt;/div&gt; &lt;input asp-for="@item.QuestionAnswer" Value="@item.QuestionId" /&gt; &lt;br /&gt; } &lt;input type="submit" class="btn btn-primary" value="ثبت"&gt; &lt;/form&gt;</pre><p> 如您所见,我使用了一个 foreach 循环,它给了我所有的“问题”。 所以我不能在我的行动中只收到一个“问题”。 这些是我的行动:</p><pre> public IActionResult Index() { return View(_context.Questions.ToList()); } [HttpPost] public IActionResult Index(Question question) { foreach (var item in question) { var ans=new Answer(){ QuestionId=item.QuestionId, AnswerTitle=item.QuestionAnswer }; _context.Answers.Add(ans); _context.SaveChanges(); } return View(_context.Questions.ToList()); }</pre><p> 我想我必须在第二个动作中得到List&lt;Question&gt;但我不知道怎么做? 任何人都可以一步一步地帮助我吗?</p></div></object> - How to get List<object> as parameter in action in ASP.NET Core MVC

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC将操作参数传递给视图 - ASP.NET MVC Passing action parameter to the view 如何获取列表<object>作为 ASP.NET Core MVC 中的参数<div id="text_translate"><p>我有一个 class,它的名字是“Question.cs”,我还有一个名字是“Answer.cs”。 我想向我的操作发送问题列表而不是一个问题,但我不知道该怎么做。 事实上,我不知道如何将问题作为参数。 谁能告诉我如何在行动中收到问题? 这是我的课程:</p><pre> public class Question { [Key] public int QuestionId { get; set; } [Required] [MaxLength(500)] public string QuestionTitle { get; set; } [Required] [MaxLength(500)] public string QuestionAnswer { get; set; } //property public List&lt;Answer&gt; Answers { get; set; } }</pre><pre> public class Answer { [Key] public int AnswerId { get; set; } [Required] public int QuestionId { get; set; } [Required] [MaxLength(500)] public string AnswerTitle { get; set; } //property [ForeignKey("QuestionId")] public Question Question { get; set; } }</pre><p> 这是我的 Razor 视图(Question.cshtml):</p><pre> @using GameShop.Data.Domain.QuestIonsAnswers @model List&lt;GameShop.Data.Domain.QuestIonsAnswers.Question&gt; @{ Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml"; } &lt;form asp-action="Index" method="POST"&gt; @foreach (var item in Model) { &lt;div&gt;@item.QuestionTitle&lt;/div&gt; &lt;input asp-for="@item.QuestionAnswer" Value="@item.QuestionId" /&gt; &lt;br /&gt; } &lt;input type="submit" class="btn btn-primary" value="ثبت"&gt; &lt;/form&gt;</pre><p> 如您所见,我使用了一个 foreach 循环,它给了我所有的“问题”。 所以我不能在我的行动中只收到一个“问题”。 这些是我的行动:</p><pre> public IActionResult Index() { return View(_context.Questions.ToList()); } [HttpPost] public IActionResult Index(Question question) { foreach (var item in question) { var ans=new Answer(){ QuestionId=item.QuestionId, AnswerTitle=item.QuestionAnswer }; _context.Answers.Add(ans); _context.SaveChanges(); } return View(_context.Questions.ToList()); }</pre><p> 我想我必须在第二个动作中得到List&lt;Question&gt;但我不知道怎么做? 任何人都可以一步一步地帮助我吗?</p></div></object> - How to get List<object> as parameter in action in ASP.NET Core MVC 如何在html动作链接中插入图像? asp.net mvc - how to insert image in html action link? asp.net mvc HTML动作链接asp.net mvc - HTML Action Link asp.net mvc 如何将方法或参数传递给ASP.Net MVC中的操作筛选器 - How to pass a method or parameter to an action filter in ASP.Net MVC 将参数传递给URL ASP.NET MVC - Passing Parameter to URL ASP.NET MVC ASP.NET MVC3在动作链接中的模型内传递模型 - Asp.net MVC3 passing model within model in an action link 将Angularjs表达式/变量作为路径参数传递给asp.net mvc操作链接 - Pass Angularjs expression/variable to asp.net mvc action link as Route Parameter 在ASP.NET MVC 3中将对象从一个操作方法传递到另一个操作方法 - Passing object from one action method to another in ASP.NET MVC 3 通过组合的动作链接和ASP.Net MVC 5中的输入将用户输入的输入从View传递到Action Controller? - Passing a user-typed input from View to Action Controller via combined action link and input in ASP.Net MVC 5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM