1

我有 aActionResults并且我想将列表字符串传递给 a partialview,但我无法弄清楚如何去做。

希望比我更聪明的人:-)可以帮助我解决它。

namespace Web.UI.Controllers
public ActionResult Index()
{
    return View();
}

public async Task<ActionResult> Test()
{
    var currentConditions   = await Task.Run(()=> _IGWC.CurrentConditions("194.61.173.5").FirstOrDefault());
    var weatherAstronomy    = await Task.Run(()=> _IGWC.WeatherAstronomy("194.61.173.5").FirstOrDefault());
    var weatherConditions   = await Task.Run(()=> _IGWC.WeatherConditions("194.61.173.5").FirstOrDefault());
    var weatherLocation     = await Task.Run(()=> _IGWL.WeatherSearch("194.61.173.5").FirstOrDefault());

    string test      = currentConditions.feelsLikeC.ToString();
    string test1     = weatherAstronomy.sunRise.ToString();
    string test2     = weatherConditions.maxtempC.ToString();
    string test3     = weatherLocation.DisplayAreaName.ToString();

    List<string> weatherData = new List<string>();
    weatherData.Add(test);
    weatherData.Add(test1);
    weatherData.Add(test2);
    weatherData.Add(test3);

    return PartialView(weatherData);
}

在包含部分视图的索引视图中,我拥有:

@{Html.Partial("Test","Home");}

最后,在我的部分观点中,我尝试使用以下内容。

What goes here

@foreach (var item in Model)
{
@item.
}

我如何从partialview获取值weatherData并传递给partialview,我发现它工作的唯一方法是使用tempdata,但我不想走那条路。

---------------------------代码更新---------- -

在索引视图中,我按要求放置了以下内容:

  @model System.Collections.Generic.List<string>

@{Html.Partial("Test", Model);}

在部分视图中,我有以下内容:

@model System.Collections.Generic.IEnumerable<string>


<ul>
@foreach (var item in Model)
{
    <li>@item</li>
}
</ul>

这现在会导致以下错误:

Message=对象引用未设置为对象的实例。由@foreach(模型中的变量项)引起

4

2 回答 2

1

Your not far away, you just need to tell the view what type of model to expect e.g.

@model System.Collections.Generic.List<string>

<ul>
@foreach (var item in Model)
{
    <li>@item</li>
}
</ul>

Update

Looking at your code you seem to be getting confused between the difference of rendering a view and actually invoking a controller action. When you call Html.RenderPartial you are asking MVC to directly render that particular view, not invoke the action on the controller. RenderPartial expects the second parameter to be the model for that view (if it applicable) - your view is expecting a List<string> but your passing in "Home" which is a string which is why you are getting errors along the line of:

The model item passed into the dictionary is of type 'System.String', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[System.String]'

What you need to do is actually invoke the action and render the resultant view e.g.

@{ Html.RenderAction("Test", "Home"); }
于 2013-01-23T10:06:46.890 回答
0

在局部视图的顶部定义

@model System.Collections.Generic.List<string>

在呈现部分视图的视图中添加

@{Html.Partial("Test", Model);}
于 2013-01-23T10:15:43.237 回答