0

I've got this code in my cshtml Details View. I want to send the VM back to this controller method. This works as I can set a breakpoint in the controller however the model is null. Here is the client-side

function downLoadFile() {

    $.ajax({
        url: '/Software/DownLoadInstall',
        type: "POST",
        data: JSON.stringify('@Model'),
        datatype: "json",
        contentType: "application/json; charset=utf-8"
     });
}

and here is what my controller looks like

 [HttpPost]
    public ActionResult DownLoadInstall(SoftwareEditViewModel vm)
    {
        try
        {
            SoftwareService.DownLoadInstall(vm);  

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
Bayrat
  • 161
  • 1
  • 3
  • 17

2 Answers2

3

I think it's unlikely that a complex model is going to produce a reasonable string representation that JSON.stringify can use. You'd be better off just passing the id of the item you want to download back (presumably contained in the model). If download means what I think, it really should return a FileResult and you shouldn't be redirecting afterwards. There's also no need to use AJAX as a file download won't take you away from the page. I'm making some big assumptions based on your naming -- if I'm wrong you really ought to use better names.

[HttpPost]
public ActionResult DownLoadInstall(int id)
{
    return SoftwareService.DownLoadInstall( ModelFromID(id) );  
}

private SoftwareViewModel ModelFromID( id )
{
    ... populate the model needed for download from id ...
}

Client

function downLoadFile() {
    location.href = '/software/downloadinstall/' + @Model.ID;
}
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • tvanfosson thx...you've got it right regarding the purpose. We have 30 dc's in our company and each country has their own local drive mapped for downloadable software. So, I've got to stream the file back using Response.WriteFile after I shell out and do my "net use" to download the file from the proper logonserver. I don't follow that I don't need to use Ajax. I've got two buttons and Details page...they can download and view the help file or they can download the actual install. Sorry if TMI i this small note. Thx for the great reply. – Bayrat Mar 17 '11 at 12:54
  • Didn't even know about FileResult until you just mentioned it. That looks like the way to go...I built the prototype of this app in traditional ASPX mode and have now redone it in MVC3...this was a huge help thanks for the reference...here is a great write up on it http://stackoverflow.com/questions/1187261/whats-the-difference-between-the-four-file-results-in-asp-net-mvc – Bayrat Mar 17 '11 at 13:09
2

Calling @Model will simply call ToString() on the object.

You will need to first convert the Model to json.

Example extension method

public static class JsonHelperExtensions
{
    static readonly JavaScriptSerializer Serializer = new JavaScriptSerializer();
    public static string ToJson(this object o)
    {
        return Serializer.Serialize(o);
    }
}

Then in your jQuery function:

function downLoadFile() {

    $.ajax({
        url: '/Software/DownLoadInstall',
        type: "POST",
        data: JSON.stringify('@Model.ToJson()'),
        datatype: "json",
        contentType: "application/json; charset=utf-8"
     });
}
Mark Coleman
  • 40,542
  • 9
  • 81
  • 101