asp.net mvc 4 - How can I return a model with an ActionLink? -


i'm trying model in view , pass controller model null when passed other controller.

controller - here send model render in view:

   [httppost]     public partialviewresult index(reportestabularesviewmodel modeloinput)     {         getdatostabularreportinput input = new getdatostabularreportinput { idsensor = modeloinput.sensor, fechainicio = modeloinput.fechainicio, fechafinal = modeloinput.fechafinal };         reportestabularesviewmodel modelo = new reportestabularesviewmodel();         var lista = new caelusreporting.datos.datosapp().getdatostabularreport(input);          var s = modelo.sensores;          viewbag.sensores = s;         modelo.datos = lista.groupby(x => x.fecha).select(y => new datosviewmodel         {             fecha = y.key,             estacionsensorsensornombre = y.first().estacionsensorsensornombre,             datos = y         }             );          viewbag.modelo = modeloinput;         return partialview(modelo);     } 

view:

@model caelusreporting.web.models.viewmodels.datos.reportestabularesviewmodel  @{      viewdata["temp"] = viewbag.modelo;         viewbag.title = model.datos.select(y => y.estacionsensorsensornombre).firstordefault();      list<caelusreporting.sensores.dto.sensoresdto> sensores = viewbag.sensores;  }      <a class="menu-bar" data-toggle="collapse" href="#menu">      <span class="bars"></span>  </a>  <div class="collapse menu" id="menu">      <div class="list-inline">          @using (ajax.beginform("index", new ajaxoptions { updatetargetid = "update", httpmethod = "post", insertionmode = insertionmode.replace }))          {                <label>sensor: </label>              <select data-placeholder="escoja las estaciones" class="chosen-select-width" tabindex="8" name="sensor">                  @foreach (var item in sensores.where(x => x.estado == true))                  {                      <option value=@item.id>@item.nombre</option>                  }              </select>              <label>fecha de inicio: </label>              @html.textboxfor(m => m.fechainicio, null, new { type = "datetime-local", style = "max-width:235px; max-height:20px" })              <label>fecha de final: </label>              @html.textboxfor(m => m.fechafinal, null, new { type = "datetime-local", style = "max-width:235px; max-height:20px" })                <input id="submit1" type="submit" value="ver" />            }            <a href="@url.action("exportreport", "reportes", new { doctype = "pdf" })"> report in pdf</a>          <a href="@url.action("exportreport", "reportes", new { doctype = "xls" })"> report in xls</a>          <a href="@url.action("exportreport", "reportes", new { doctype = "doc" })"> report in doc</a>          <a href="@url.action("exportreport", "reportes", new { doctype = "csv" })"> report in csv</a>                        </div>  </div>

another controller - here try import report in:

  public actionresult exportreport(string doctype)     {         reportestabularesviewmodel test = viewdata["temp"] reportestabularesviewmodel;          getdatostabularreportinput input = new getdatostabularreportinput { idsensor = test.sensor, fechainicio = test.fechainicio, fechafinal = test.fechafinal };          var lista = new caelusreporting.datos.datosapp().getdatostabularreport(input);         reportdocument rd = new reportdocument();         rd.load(path.combine(server.mappath("~/reportess"), "reporte.rpt"));         rd.setdatasource(lista);         response.buffer = false;         response.clearcontent();         response.clearheaders();          switch (doctype)         {             case "pdf":                 rd.load(path.combine(server.mappath("~/reportess"), "reporte.rpt"));                 try                 {                     stream stream = rd.exporttostream(crystaldecisions.shared.exportformattype.portabledocformat);                     stream.seek(0, seekorigin.begin);                     return file(stream, "application/pdf", "reportepdf.pdf");                 }                 catch (exception ex)                 {                     throw;                 }             case "doc":                 rd.load(path.combine(server.mappath("~/reportess"), "reporte.rpt"));                 try                 {                     stream stream = rd.exporttostream(crystaldecisions.shared.exportformattype.wordforwindows);                     stream.seek(0, seekorigin.begin);                     return file(stream, "application/msword", "reportedoc.doc");                 }                 catch (exception ex)                 {                     throw;                 }             case "xls":                 rd.load(path.combine(server.mappath("~/reportess"), "reporte.rpt"));                 try                 {                     stream stream = rd.exporttostream(crystaldecisions.shared.exportformattype.excel);                     stream.seek(0, seekorigin.begin);                     return file(stream, "application/vnd.ms-excel", "reportedoc.xls");                 }                 catch (exception ex)                 {                     throw;                 }             case "csv" :                  rd.load(path.combine(server.mappath("~/reportess"), ""));                 try                 {                     stream stream = rd.exporttostream(crystaldecisions.shared.exportformattype.characterseparatedvalues);                     stream.seek(0, seekorigin.begin);                     return file(stream, "text/csv", "reportecsv.csv");                 }                 catch (exception ex)                 {                     throw;                 }             default:                 return null;         }     } 

you can not pass viewdata between requests (actions). need serialize data somehow, in querysting example. can use routevaluedictionary this.

you need create model action actionresult exportreport(string doctype) this:

public class exportreportmodel {     public string doctype {get; set;}     // fields required reportestabularesviewmodel } 

then action looks actionresult exportreport(exportreportmodel model) , can render such links:

<a href="@url.action("exportreport", "reportes", new routevaluedictionary(new reportestabularesviewmodel{/*initialize object*/}))">get  report in pdf</a> 

you can use anonymous objects, in case when have more 3 parameters organize in kind of structure.


Comments