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.

7
  • 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. Commented Jul 22, 2014 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 Commented Jul 22, 2014 at 13:46
  • Oh my goodness how come I missed it, In fact I tried it that too, Let me try again :) Commented Jul 22, 2014 at 13:46
  • @YuliamChandra then it will be a problem for me to get it in Dialog right? Commented Jul 22, 2014 at 13:48
  • @TBA : Did you able to get the value as a string ? Commented Jul 22, 2014 at 13:53

1 Answer 1

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}),

});

Sign up to request clarification or add additional context in comments.

3 Comments

What about the Dialog Part, How can I achieve that using Ajax.
To get view in dialog use, jquery and fill values of the array when dialog loads, stackoverflow.com/questions/17613151/…
Thats why I tried to use @Html.ActionLink, but with Ajax post how could that be possible?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.