[英]Pass js complex array to mvc controller, and then return another view
這與我之前的問題有關。 我想知道,即使現在我正在嘗試發送一個更復雜的數組,我是否可以以相同的方式傳遞 JS 數組(作為上一個問題中接受的答案):
如果是,那么如何? 在控制器中接收時獲取空值。 這是我的模型:
public class QuestionModel
{
public bool Choice { get; set; }
public List<object> ChoiceQuestions { get; set; } //can i use List<object>?
public int Id { get; set; }
public string Question { get; set; }
public bool Short { get; set; }
public object ShortQuestion { get; set; } //here aswell - can I use object?
public string Type { get; set; }
}
甚至不確定我是否可以重用 js 代碼將數據發送到答案中給出的控制器。 如果不是,那么我應該如何解決這個問題? 我仍然必須能夠通過 post 將數據發送到控制器,然后在處理數據后將其返回到視圖。
你可以做任何對象作為一個隱藏的表單字段的JSON和然后張貼的形式與字符串參數動作。 然后在服務器端將 json 消毒到 action 方法內的對象 - 即使在另一個控制器中。 您可以從控制器方法返回不同的視圖。
Javascript (在股票頁面/視圖上說)
var form = document.createElement("form");
var input = document.createElement("input");
input.setAttribute("type", "hidden") ;
input.setAttribute("name", "outwardStocks") ;
input.setAttribute("value", JSON.stringify(selectedrecords));
form.appendChild(input);
document.body.appendChild(form);
form.action = "/CRM/Invoice/Index";
form.method = "post"
form.submit();
控制器代碼(比如發票控制器)
[HttpPost]
public IActionResult Index(string outwardStocks)
{
InvoiceItems invItems = JsonSerializer.Deserialize<InvoiceItems>(outwardStocks);
......
return View("Invoice" InvoiceVM);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.