0

everyone! I'm learning ASP.NET MVC and have some question. My problem is Passing Data from View to Controller.

This my Code:

@{
    string listID = "";
}

and I try to use this variable:

function SubmitDelete() {
   var listId = "";        
   var x = document.getElementsByName("IsCheck");
   for (var i = 0; i < x.length; i++) {
     if (x[i].checked == true) {
        listId += x[i].value + ", ";
     };
   }
   @listID = listId;
   return listId;
}

Finalize, I want to pass @listID to Controller:

@using (Html.BeginForm("DeleteChecked", "Category", new { listID }, FormMethod.Post)){ }

It is simple problem about multi delete with checkbox. Help me, please.

2
  • I think you need to learn the distinction between client- and server-side code and at what point each is run. Your question makes very little sense as it stands. Commented Jun 6, 2014 at 10:15
  • You could use a hidden field, then select in the SubmitDelete() using document.getElementById("HiddenFieldId") and set it's value to listId. Then you can pass the value of your hidden field in your controller Commented Jun 6, 2014 at 10:15

2 Answers 2

2

You cannot pass a javascript variable to your controller.

But you can post it as part form data with the help of hidden field.

Better add a hidden field and set it in a Javascript and post

@using (Html.BeginForm("DeleteChecked", "Category", FormMethod.Post)){ 

 Html.HiddenFieldFor(m=>m.MyList, new {@id="my-list-data"})

 ..other controls and your submit button

}

In a Javascript

function SubmitDelete() {
   var listId = "";        
   var x = document.getElementsByName("IsCheck");
   for (var i = 0; i < x.length; i++) {
     if (x[i].checked == true) {
        listId += x[i].value + ", ";
     };
   }
   $('#my-list-data').val(listId);

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

5 Comments

Why are you still passing the empty listID string as a route parameter?
@AntP, I didn't notice it. Updated now.
thanks for your answer. But what is MyList in m=>m.MyList. I use IList for Model
@BaTrinh, Please add your controller action method with the model detail
Hey, in your answer: Html.HiddenFieldFor(m=>m.MyList, new {@id="my-list-data"}). The 2th parameter is htmlAttibutes. Exactly, I add value in this position: easest is "". Then, this HiddenField will get value from JS tab. And my problem was resolved. Thanks u very much
0

You cannot do that.
The aspx\ascx\cshtml etc. page is built in the server side while the js is computed on the client's side.
You can add C# string to js functions but they will be hard coded when they get to the client.
All the C# expression are evaluated before they get to the client and before the js is computed.

Here's an example: This is what you see on the aspx\ascx\cshtml file.

<%
string str = 'test';
%>
function jsFunc(){
var myVar = '<%=str%>';
}

This is what the client gets:

function jsFunc(){
var myVar = 'test';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.