0

This is related to my previous question. I'm wondering, can I pass a JS array the same way (as the accepted answer in previous question) even if now I'm trying to send a bit more complex array: enter image description here

If yes, then how? Getting nulls while receiving in controller. this is my model:

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; }
}

Not even sure if I can reuse the js code for sending the data to controller that was given in the answer. If no, then how should I solve this? Still I have to be able to send the data via post to controller, and then after processing data return it back to the view.

Denis Wasilew
  • 503
  • 3
  • 9
  • 29
  • How are you sending it ? You can post a complex object via ajax and the response of your action method(could be a view result) will come back to the success handler of the ajax call. If you want to navigate to a different page,ajax might not be the right solution here. Do a normal form post. – Shyju Nov 20 '17 at 15:45
  • 1
    If it is an `object`, you (or the MVC binder) cannot set any properties. Maybe `dynamic` works, or create a real class for those questions. – Hans Kesting Nov 20 '17 at 18:29
  • 1
    Are you posting this as a normal submit or using ajax. If a normal submit, then you cannot use an array. You must use the correct names with collection indexers, for example if the POST methid is `List`, then your would need `[0].Choice: true`, `[0].ChoiceQuestions[0].Id: 0` etc –  Nov 20 '17 at 22:11
  • 1
    Its not clear why your building a JavaScript array of objects if your wanting to do a normal submit and redirect - you do not need to do that, or any javascript at all) if you have generated your view correctly in the first place –  Nov 20 '17 at 22:13
  • And do not use `object` - it should be `public List ChoiceQuestions` where `ChoiceQuestionsVM` is a class containing properties `Id` and `Value` (ditto for `public object ShortQuestion`) –  Nov 20 '17 at 22:15
  • Shyju, yes ajax is not the solution here. @StephenMuecke Hmm... will normal submit work here? I will probably have up to even 50 of these complex arrays (as in the image). I'm building a JS array because it was easier for me to handle all the values of the array from many fields. – Denis Wasilew Nov 20 '17 at 22:31
  • 1
    If you have generated you view correctly, then yes a normal submit will work. For example refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) for how to generate form controls for models which are a collection (or models containing properties which are collections). Or if your dynamically adding collection items in a view, refer [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) –  Nov 20 '17 at 22:35
  • Thanks I will have a look on that. – Denis Wasilew Nov 21 '17 at 07:18

1 Answers1

0

You may make Json of any object as a single hidden form field and then post the form to action with string parameter. Then on server side desterilize json to the object inside the action method - even in another controller. You may return different view from the controller method.

Javascript (Say on Stock Page/View)

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();

Controller code (Say Invoice Controller)

[HttpPost]
public IActionResult Index(string outwardStocks)
{
   InvoiceItems invItems = JsonSerializer.Deserialize<InvoiceItems>(outwardStocks);
   ......
   return View("Invoice" InvoiceVM);
}
Bharat Vasant
  • 850
  • 3
  • 12
  • 46