I want to create some view with filters and data table that will by filtered. The problem is with the filters because they are created dynamically.
public class TestController : Controller
{
public ActionResult Test()
{
DisplayModel model = new DisplayModel();
model.Filters = new List<TestFilter>() { new TestFilter() { Name = "Name 1" }, new TestFilter() { Name = "Name 2" }, new TestFilter() { Name = "Name 3" } };
return View(model);
}
public ActionResult JsonChange(List<TestFilter> filters)
{
if (filters == null || filters.Count == 0) return PartialView("_Selected", null);
SelectedModel model = new SelectedModel();
model.SelectedValues = "";
foreach (var el in filters)
{
model.SelectedValues += (el.Name + " " + el.Value + "<br />");
}
return PartialView("_Selected", model);
}
}
Here are my main View
@model TestReport.Models.DisplayModel
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$(".submit").click(function () {
$.ajax({
url: '/Test/JsonChange/',
data: '<what should be here>',
success: function (data) {
$("#content").html(data);
}
});
});
});
</script>
@foreach (var el in Model.Filters)
{
<div>开发者_StackOverflow中文版
<span>@el.Name</span><span>@Html.TextBoxFor(t => el.Value, null)</span>
</div>
}
<div class="submit" style="border:1px solid black;width:100px;">
Send</div>
and here is my partial view that will be changed by ajax calls:
@model TestReport.Models.SelectedModel
You have selected:
<br />
@if (Model != null && !String.IsNullOrEmpty(Model.SelectedValues))
{
<text>@Model.SelectedValues</text>
}
finally there are my Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TestReport.Models
{
public class SelectedModel
{
public string SelectedValues { get; set; }
}
}
and Filter class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TestReport.Entities
{
public class TestFilter
{
public string Name { get; set; }
public string Value { get; set; }
}
}
I tried to simplify whole solution to present you my problem. What i want to accomplish is to Send to controller method all the values of dynamically created input boxes with ajax as object (List). I know one of the approach is to use $.ajax({}) method. But maybe it`s not the best solution?
here is a post that probably explains about your scenario
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
精彩评论