I have the following code which makes a query to my database and displays the data in an html table:
public JsonResult DataJson()
{
List<MyClass> data = bd.Database.SqlQuery<MyClass>("Select * from mytabla").ToList();
return Json(data, JsonRequestBehavior.AllowGet);
}
MyClass
public class MyClass
{
public int Usuario { get; set; }
public DateTime Fecha { get; set; }
public int Semanas { get; set; }
public double Cantidad { get; set; }
public string Concepto { get; set; }
public int Semana { get; set; }
}
And with ajax I receive them and show the results in my view:
<script type="text/javascript">
$(document).ready(function ()
{
Fill_tabla();
});
function Fill_tabla() {
$.getJSON("/MyController/DataJson", function (data) {
$.each(data, function (idx, opt) {
$('#datatable').append('<tr><td>' + opt.column1 + '</td><td>' + opt.column2 + '</td></tr>');
});
}, 'json');
}
</script>
Now the following code sends an array with the data from my HTML table to the controller to update them in the database so far so good, the problem is that it shows the error:
The model element passed to the dictionary is of type .JsonResult, but this dictionary requires an IEnumerable.
Am I supposed to convert the array to IEnumerable? or as?
I clarify that both functions (ajax and controller) are repeated for each row of my table.
function GuardarF() {
$.ajax({
type: 'POST',
dataType: "json",
url: '/MyController/MyView',
traditional: true,
data: {
array: array
},
success: function (data) {
if (data.rstProceso === "true") {
console.log('Exito: ' + data.rstMensaje);
} else if (data.rstProceso === "false") {
console.log('Algo salió mal ' + data.rstMensaje);
}
},
error: function (jqXHR, status, err){
console.log(jqXHR.responseText);
}
});
}
[HttpPost]
public ActionResult MyView(int[] array)
{
JsonResult dtaEjecucionTarea = default(JsonResult);
if (Update_MyView(array))
{
dtaEjecucionTarea = Json(new
{
rstProceso = "true",
MessageGestion = "Cambios guardados con éxito"
});
}
else
{
dtaEjecucionTarea = Json(new
{
rstProceso = "false",
MessageGestion = "Error, algo salió mal, intente de nuevo"
});
}
return View(dtaEjecucionTarea);
}
public bool Update_MyView(int[] array)
{
if (array != null)
{
//my code
bd.SaveChanges();
return true;
}
else
{
return false;
}
}
My model
public partial class MyModel
{
public int Id { get; set; }
public Nullable<int> Usuario { get; set; }
public Nullable<System.DateTime> Fecha { get; set; }
public Nullable<int> Semanas { get; set; }
public Nullable<double> Cantidad { get; set; }
public string Concepto { get; set; }
public Nullable<int> Semana { get; set; }
}
Console error message:
If you look at what you return in the method, you are passing a JSONResult as a parameter to your view (MyView.cshtml)
When you do return without indicating a view name, the system understands that the view it has to get is the one called in the same way as the method you are executing in the controller, in this case "MyView.cshmtl"
You have to check in your view, when you indicate the model it uses, what class it is and pass that class to it.
The error is that, your view expects an object of a specific class but you are passing it a JSONResult
If what you want is to return directly what you are filling in the if:
Your return has to be like this
Or in your case: