0

In my model I have these properties,

public string[] SelectedIDs {get; set;}
public int BookId {get;set;}
public int LanguageId {get;set;}

In my view I have the following,

@Html.HiddenFor(model => model.SelectedIDs )
@Html.HiddenFor(model => model.BookId )
@Html.HiddenFor(model => model.LanguageId )

Using a JavaScript array (SelectedItemIds) I am updating the SelectedIDs,

$a("#SelectedIDs").val(SelectedItemIds.join());
alert($a("#SelectedIDs").val());

The alert successfully returning comma separated values. If I do a post to my action no problem, I am getting the SelectedIDs as comma seperated. However my requirement was to populate the view in a dialog, However while passing I am always getting SelectedIDs as null.

I tried with two of the following methods.

Method 1:

My action is:

public ActionResult MyMethod1(MyModel model)
{
}

And I used the action link as

@Html.ActionLink("My Book", "MyMethod1", "BookOrder", new { @id = "SubmitBooks", @class = "subBook", data_dialog_id = "AddBookDialog", data_dialog_title = "Add Books", data_dialog_width = 800, data_dialog_height = 550 }) 

In this method I successfully get model.BookID as well as model.LanguageId, but model.SelectedIDs was null.

Method 2:

My action is:

public ActionResult MyMethod2(string[] selectedItems, int bookId, int langId)
{
}

And I used the action link as

@Html.ActionLink("My Book", "MyMethod2", "BookOrder", new { @selectedItems= Model.SelectedIDs , @bookId= Model.BookId, @langId = Model.LanguageId }, new { @id = "SubmitBooks", @class = "subBook", data_dialog_id = "AddBookDialog", data_dialog_title = "Add Books", data_dialog_width = 800, data_dialog_height = 550 }) 

Here too I got both bookid and langId but no selectedItems, which is null.

TBA
  • 1,077
  • 5
  • 41
  • 80
  • In `Method 1` your model's `SelectedIDs` should be a `string` if you are using a `ActionLink` and in `Method 2`, `selectedItems` should be a `string`. Then you can use `Split` method inside your controller to create an `array` if you want one. – Saranga Jul 22 '14 at 13:41
  • when you modified the hidden field, the a href parameter doesn't change since it's rendered, you should send it programmatically, register to click event, then use jquery ajax – Yuliam Chandra Jul 22 '14 at 13:46
  • Oh my goodness how come I missed it, In fact I tried it that too, Let me try again :) – TBA Jul 22 '14 at 13:46
  • @YuliamChandra then it will be a problem for me to get it in Dialog right? – TBA Jul 22 '14 at 13:48
  • @TBA : Did you able to get the value as a string ? – Saranga Jul 22 '14 at 13:53
  • @Saranga yes I tried but no luck ... – TBA Jul 22 '14 at 13:56
  • @TBA, is it [jquery dialog](http://api.jqueryui.com/dialog/)? – Yuliam Chandra Jul 22 '14 at 14:11

1 Answers1

1

You need to set traditional: true when serializing arrays.

$.ajax({
type: "POST",
traditional: true,
url: "../BookOrder/MyMethod2",
data: { function_param: SelectedIDs}

});

Found this good explanation on what traditional: true does: https://stackoverflow.com/a/5497151/2419531

If you don't want to use traditional: true, you can pass the data as string using JSON.stringify and specifying the contentType:

$.ajax({
type: "POST",
url: "../BookOrder/MyMethod2",
contentType: 'application/json',
data: JSON.stringify({function_param: SelectedIDs}),

});

Community
  • 1
  • 1
Brijesh
  • 352
  • 5
  • 17
  • What about the Dialog Part, How can I achieve that using Ajax. – TBA Jul 22 '14 at 13:47
  • To get view in dialog use, jquery and fill values of the array when dialog loads, http://stackoverflow.com/questions/17613151/open-jquery-dialog-box-on-click-on-html-actionlink-mvc4 – Brijesh Jul 22 '14 at 13:51
  • Thats why I tried to use @Html.ActionLink, but with Ajax post how could that be possible? – TBA Jul 22 '14 at 13:57