Asp.net mvc 模型从PartialView发送到控制器

Asp.net mvc 模型从PartialView发送到控制器,asp.net-mvc,Asp.net Mvc,我开始在asp.net-mvc中工作,在将模型从partialview发送到控制器时遇到问题。 首先,这是我创建partialview的方式 @Html.Partial("Weather", ShopB2B.Controllers.HomeController.GetWeather()) GetWeather()是将第一个数据初始化为模型的控制器方法。模型看起来像这样 public class Weather_m { public IEnumerable<Sel

我开始在asp.net-mvc中工作,在将模型从partialview发送到控制器时遇到问题。 首先,这是我创建partialview的方式

@Html.Partial("Weather", ShopB2B.Controllers.HomeController.GetWeather())
GetWeather()是将第一个数据初始化为模型的控制器方法。模型看起来像这样

public class Weather_m
    {

        public IEnumerable<SelectListItem> City_dropdown { get; set; }

        public string Temperature { get; set; }
    }
@model ShopB2B.Models.Weather_m

@using (@Html.BeginForm("GetWeatherNew", "Home", new { weather = Model }, FormMethod.Post))
{
    <table>
        <tr>
            <td>@Html.DropDownListFor(x => x.City_dropdown, Model.Miasta_dropdown)</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(x => x.Temperature, Model.Temperatura)</td>
            <td><<input type="submit" value="Send" class="submitLink" style=" height: 40px;" /></td>
        </tr>
        </table>
    }
公共类天气
{
公共IEnumerable City_下拉列表{get;set;}
公共字符串温度{get;set;}
}
这是nesesery的下拉列表,partialview看起来像这样

public class Weather_m
    {

        public IEnumerable<SelectListItem> City_dropdown { get; set; }

        public string Temperature { get; set; }
    }
@model ShopB2B.Models.Weather_m

@using (@Html.BeginForm("GetWeatherNew", "Home", new { weather = Model }, FormMethod.Post))
{
    <table>
        <tr>
            <td>@Html.DropDownListFor(x => x.City_dropdown, Model.Miasta_dropdown)</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(x => x.Temperature, Model.Temperatura)</td>
            <td><<input type="submit" value="Send" class="submitLink" style=" height: 40px;" /></td>
        </tr>
        </table>
    }
@model shopbb.Models.Weather\m
@使用(@Html.BeginForm(“GetWeatherNew”,“Home”,new{weather=Model},FormMethod.Post))
{
@Html.DropDownListFor(x=>x.City\u下拉列表,Model.Miasta\u下拉列表)
@LabelFor(x=>x.Temperature,Model.temperaturea)

您应该适当地定义属性bind to Dropdown。由于您已将city_下拉列表定义为IEnumarable,因此在从数据发送到服务器时,由于数据类型不匹配,模型绑定将失败(在客户端,city_下拉列表将生成为select控件的字符串)。在这种情况下,您应该按如下方式更改模型的属性

public class Weather_m
{

    public string City_dropdown { get; set; }

    public string Temperature { get; set; }
}


您确实不应该在视图渲染时获取ViewModel类型的数据

您的数据类型为
shopbb2.Models.Weather\m
。您的强类型局部视图需要这样做,这一切都很好。但不是使用
shopbb2.Models.Weather\m
指示您的
shopbb2.Controllers.HomeController.GetWeather()
,您应该创建一个ViewModel并将其返回到强类型视图,比如说
MyViewModel
。这应该包装一个
ShopB2B.Models.Weather\m
的实例。因此,在主视图中,您的视图将被强类型化为:

@model ShopB2B.Models.MyViewModel
你把你的局部视图渲染成

@Html.Partial("Weather", Model.MyWeather_m)
我通常也会将局部视图包装在表单中,如:

@using (@Html.BeginForm("GetWeatherNew", "Home", new { weather = Model }, FormMethod.Post))
{
    @Html.Partial("Weather", Model.MyWeather_m)
}

希望这有帮助。

好的建议。你的回答中只有一件事让我不安:
new{weather=Model}
在构建HTML表单时。不要忘记路由值使用简单属性,而不是复杂对象。好的A.Burak Erbora,但问题是我在may main view中有其他viewmodel,所以我必须在其他视图中将此模型添加到partialview中way@cyprian:为什么不在主视图的viewmodel中添加天气模型呢?毕竟,您需要在最终呈现的页面中删除所有这些数据。因为当有人选择DropDownList时,此模型会一直更改。如果此表单将DropDownList中的更改信息发送给cotroller,他会添加一些新信息并再次发送。因此,这就是我现在不知道如何执行此操作的原因。听起来您希望使用ajax进行t帽位而不是局部视图。我建议您查看一下:-)@Html.DropDownListFor(x=>x.City_下拉列表,Model.Miasta_下拉列表)这是不可能的有两个模型吗?