In response to your first question, try this Url.Action overload:
Url.Action("ActionName","ControllerName")
And your second question: you will want to do this through AJAX.
Basically, the first time the page loads, return everything you want to display on the screen. Then we have an ajax function of this form:
<script type="text/javascript"> function SendErrorsOrWhatever() { dummy = 'error: myError, someOtherProperty = property'; //this of course assumes that 'myError' and 'property' exist $.ajax({ type: 'GET', //i changed this because it actually probably better suits the nature of your request url: '../Details/Send', cache: false, //(probably) dataType: 'json', //(probably) data: dummy, //json object with any data you might want to use success: function(result) { alert("The error has been transferred"); //you can also update your divs here with the `result` object, which contains whatever your method returns } }); } </script>
A few notes: 1) A URL is automatically added to include data from data
. 2) Now your action method should accept a parameter. There are many ways around this. I added an example of how your action method might look in the code below.
and then your onclick
button will call this function, for example:
<input type="button" onclick='SendErrorsOrWhatever()'/>
What happens is that when you click the Ajax button, it will delete your action method asynchronously (by default), and your action method will do everything it needs with errors, and then when it is completed, the success
will be deleted .
EDIT: now your action might look something like this:
[AcceptVerbs(HTTP.Get)] public static Error GetErrors(Error error) {
Now this assumes that the JSon dummy
object has the same fields as the Error
class. If so, MVC will automatically bind data to it. You can also use a more general FormCollection
for the parameter. Indeed, you can use whatever you want, but you want to wrap whatever you want in a JSon object.
Edit - for your last question this should work:
Add the following extension method to the application:
public static class JsonExtensions { public static string ToJson(this Object obj) { return new JavaScriptSerializer().Serialize(obj); } }
Or, if you want, you can simply add the ToJson () method to your model class, and then:
var model = @Model.ToJson();
in js right before the ajax call. Then use the model
variable for your data
parameter.