在下拉列表中选择项目时,MVC4更改数据对象

时间:2014-09-09 18:49:03

标签: c# asp.net-mvc entity-framework

我对MVC很新,我没有想出正确的问题来从谷歌那里得到答案,所以我在这里。

我有一个下拉列表框,其中包含一个'列表'与客户相关联的地址,在此页面上,我希望能够点击其中一个地址并使用所选的“ID”#39;从列表中获取正确的地址模型以填充所有地址字段,并在选择其他地址时切换。这可以通过绑定来完成,还是我必须做很多手册'重置值?

<table>
    <tr>
        <td colspan="2">
            <div class="form-group">
                @Html.LabelFor(model => model.SelectedLocation.CityState, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(model => model.Location.City
                                            , Model.Carrier.Locations.Select(x => new SelectListItem { Value = x.ID.ToString(), Text = x.CityState })
                                            , new { @rows = 6, @multiple = false, style = "width: 300px;", onchange = "onChangeLocationDDL(this)" })
                </div>
            </div>
        </td>
    </tr>
    <tr>
        <td valign="top">
            <div class="form-group">
                @Html.LabelFor(model => model.SelectedLocation.Address1, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.SelectedLocation.Address1)
                    @Html.ValidationMessageFor(model => model.SelectedLocation.Address1)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.SelectedLocation.Address2, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.SelectedLocation.Address2)
                    @Html.ValidationMessageFor(model => model.SelectedLocation.Address2)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.SelectedLocation.City, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.SelectedLocation.City)
                    @Html.ValidationMessageFor(model => model.SelectedLocation.City)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.SelectedLocation.State, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.SelectedLocation.State)
                    @Html.ValidationMessageFor(model => model.SelectedLocation.State)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.SelectedLocation.Zip, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.SelectedLocation.Zip)
                    @Html.ValidationMessageFor(model => model.SelectedLocation.Zip)
                </div>
            </div>

        </td>
        <td valign="top">

            <div class="form-group">
                @Html.LabelFor(model => model.SelectedLocation.Country, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.SelectedLocation.Country)
                    @Html.ValidationMessageFor(model => model.SelectedLocation.Country)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.SelectedLocation.Latitude, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.SelectedLocation.Latitude)
                    @Html.ValidationMessageFor(model => model.SelectedLocation.Latitude)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.SelectedLocation.Longitude, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.SelectedLocation.Longitude)
                    @Html.ValidationMessageFor(model => model.SelectedLocation.Longitude)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.SelectedLocation.LocationType, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(model => model.SelectedLocation.LocationType, (new CarrierDirectoryWeb.Models.LocationTypes()).ToSelectList())
                    @Html.ValidationMessageFor(model => model.SelectedLocation.LocationType)
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-10">
                    @Html.HiddenFor(model => model.SelectedLocation.CarrierID)
                </div>
            </div>
        </td>
    </tr>
  </table>

1 个答案:

答案 0 :(得分:1)

如果您的确定命中控制器以更新页面的一部分,请执行以下操作:

- 为您的html创建一个部分视图,负责显示地址。

- 您的原始视图调用此部分并传入地址模型。

- 创建一个将采用Id并返回PartialViewResult的动作。

-pass onchange作为你的下拉列表的html属性,使用jquery来调用action方法并替换包含地址的div。