问题
I'm using the Telerik PanelBar to do some asynchronous loading using a partial view. I'm creating a model for the partial view in a parent view, but for some reason my data isn't coming through in tact.
// Parent view
<% Html.Telerik().PanelBar().Name("PanelBar").HtmlAttributes(new { style = "padding-left: 0em;" }).Items(items =>
{
foreach (var item in Model.Visits)
{
SiteVisitDetailModel model = new SiteVisitDetailModel();
model.URL = item.Key; // this is properly set
model.Dates = new List<DateTime>(); // this is null in the controller
model.Dates.Add(DateTime.Now);
items.Add()
.Text(item.Key.ToString() + " " + item.Count().ToString() + " visits")
.LoadContentFrom("SiteViewDetail", "Report", model);
}
}).Render();
// Report controller method
public ActionResult SiteViewDetail(SiteVisitDetailModel model)
{
return PartialView(model); // model.URL is correct, model.Dates is null
}
// Model
public class SiteVisitDetailModel
{
public String URL
{
get;
set;
}
public List<DateTime> Dates
{
get;
set;
}
}
As suggested by my comments, when the controller's SiteVisitDetail method is called, Model.URL has the correct data, and Model.Dates is null (it's not a list containing null, it itself is null). It, as would be expected, is also null in the partial view (SiteViewDetail).
What would cause this behavior?
回答1:
I've not used Telerik MVC controls yet myself but looking at the API for LoadContentFrom it seems this method (and all its overload) will do a GET request at a certain URL. The overload that takes an object to pass parameters more then likely doesn't know how to serialize Lists (inspecting HTTP traffic will give more details).
Your only option is to use the LoadContentFrom(String)
method and pass it a formatted list of dates as string
:
var dateArray = model.Dates.Select(d => d.ToString()).ToArray();
var serializedDateString = String.Join("#", dateArray);
items.Add()
.Text(item.Key.ToString() + " " + item.Count().ToString() + " visits")
.LoadContentFrom(Url.Action("SiteViewDetail","Report" new { dates = serializedDateString, url = model.URL}))
Then your controller should look something like this:
public ActionResult SiteViewDetail(string dates, string url)
{
SiteVisitDetailModel model = new SiteVisitDetailModel();
//split dates back into a List<DateTime>
model.Dates = dates.Split('#').Select(s => DateTime.Parse(s)).ToList<DateTime>();
model.URL = url;
return PartialView(model);
}
It's a hack (or at least not very pretty) but it'll work.
回答2:
It turns out the query string being built isn't correct. It's generating something like:
?value1=somevalue&value2=whatever
So while the value is there and correct, it's not getting parsed properly because of that amp;.
I'm guessing this is a problem with the Telerik control. I'll report the bug and see what they say.
Since that appears to be the problem, I'm just going to concatenate everything into one big string and parse it in the controller, until a more formal solution can be found.
Thanks for your help, Martijn.
来源:https://stackoverflow.com/questions/2044547/mvc-model-missing-data-in-partial-view