MVC-3 DropdownList 或 DropdownListFor - 无法在控制器 POST 中保存值

发布于 2024-12-19 10:56:37 字数 2927 浏览 4 评论 0原文

我搜索了几个小时(或几天),但还没有找到解决方案。我想使用 DropdownListFor 编辑客户,以获得具有正确预选值的称呼。

我有 3 个实体(数据库优先概念,这不是我自己的设计...):客户、地址、称呼

客户有一个 address_id (f_key),而地址有一个 salutation_id (f_key)。例如,ADDRESS 包含名字和姓氏。在 SALUTATION 实体内部有一个列 sal1,它保存所有可能的称呼。

现在,我想通过 ViewModel 编辑我的客户,如下所示:

public class CustomerViewModel
{
    public CUSTOMER cust { get; set; }
    public SelectList salutationList { get; set; }

    CustomerRepository repository = new CustomerRepository();

    public CustomerViewModel(int id)
    {
        cust = repository.GetCustomerByIdAsQueryable(id).Single();
        salutationList = new SelectList(repository.GetSalutations(), cust.ADDRESS.SALUTATION.SAL1);
    }
    // Some more
}

CutsomerRepository 方法:

public class CustomerRepository
    {
        private MyEntities db = new MyEntities();

        public IQueryable<CUSTOMER> GetCustomerByIdAsQueryable(int id) {...}

        public IQueryable<CUSTOMER> GetCustomersByName(string firstName, string lastName, int maxCount)  {...}

        public List<string> GetSalutations()
        {
            var salutationList = new List<string>();
            var salutationListQry = from s in db.SALUTATION
                                    select s.SAL1;
            salutationListTemp.AddRange(salutationListQry);
            return salutationList;
        }
        // ...
    }

这是我的控制器方法:

[HttpPost]
public ActionResult CustomerData(int id, FormCollection fc)
{
    var vm = new CustomerViewModel(id);
    // Why do I need the following line?
    vm.cust = repository.GetCustomerByIdAsQueryable(id).Single();
    try
    {
        UpdateModel(vm, fc);
        repository.Save();
        return View("CustomerData", vm);
    }
    catch (Exception e)
    {
        return View();
    }
}

最后是我的视图中的部分:

@model WebCRM.ViewModels.CustomerViewModel
@using (Html.BeginForm())
{
    // ...
    <div class="editor-label">
        @Html.Label("Salutation:")
        @Html.DropDownListFor(model => model.cust.ADDRESS.SALUTATION.SAL1, Model.salutationList)
        // @Html.DropDownList("Salutation", Model.salutationList)
    </div>
    <div class="editor-label">
    </div>
    <div class="editor-field">
        @Html.Label("Last name:")
        @Html.EditorFor(model => model.cust.ADDRESS.LASTNAME)
        @Html.ValidationMessageFor(model => model.cust.ADDRESS.LASTNAME)
    </div>
    <p>
        <input type="submit" value="Speichern" />
    </p>
}

更改和保存姓氏工作正常。但是,在保存称呼时,它会将 SALUTATION 实体中的 SAL1 值更改为我在 DropdownListFor 中选择的值。我想要的是更改我的客户的 ADDRESS 实体内的 salutation_id 。为什么这不起作用?

另一个奇怪的行为:当删除我的 CustomerController 中的标记行时,我什至无法更改和保存姓氏。通常,CustimerViewModel 的构造函数会设置客户。那么为什么我必须在 ViewModel 中添加用于设置客户的代码行呢?它是重复的,但必须在那里......

提前致谢。

I searches for hours (or days) and didn't find a solution yet. I want to edit a customer with a DropdownListFor for the salutation with the right preselected value.

I've got 3 entities (Database first concept, this is not my own design...): customer, address, salutation

A CUSTOMER has an address_id (f_key) and an ADDRESS has got a salutation_id (f_key). The ADDRESS holds the first and last name for example. Inside the SALUTATION entity there is a column sal1 which holds all possible salutations.

Now, I want to edit my customer via a ViewModel which looks like this:

public class CustomerViewModel
{
    public CUSTOMER cust { get; set; }
    public SelectList salutationList { get; set; }

    CustomerRepository repository = new CustomerRepository();

    public CustomerViewModel(int id)
    {
        cust = repository.GetCustomerByIdAsQueryable(id).Single();
        salutationList = new SelectList(repository.GetSalutations(), cust.ADDRESS.SALUTATION.SAL1);
    }
    // Some more
}

The CutsomerRepository methods:

public class CustomerRepository
    {
        private MyEntities db = new MyEntities();

        public IQueryable<CUSTOMER> GetCustomerByIdAsQueryable(int id) {...}

        public IQueryable<CUSTOMER> GetCustomersByName(string firstName, string lastName, int maxCount)  {...}

        public List<string> GetSalutations()
        {
            var salutationList = new List<string>();
            var salutationListQry = from s in db.SALUTATION
                                    select s.SAL1;
            salutationListTemp.AddRange(salutationListQry);
            return salutationList;
        }
        // ...
    }

This is my controller method:

[HttpPost]
public ActionResult CustomerData(int id, FormCollection fc)
{
    var vm = new CustomerViewModel(id);
    // Why do I need the following line?
    vm.cust = repository.GetCustomerByIdAsQueryable(id).Single();
    try
    {
        UpdateModel(vm, fc);
        repository.Save();
        return View("CustomerData", vm);
    }
    catch (Exception e)
    {
        return View();
    }
}

And finally the part from my View:

@model WebCRM.ViewModels.CustomerViewModel
@using (Html.BeginForm())
{
    // ...
    <div class="editor-label">
        @Html.Label("Salutation:")
        @Html.DropDownListFor(model => model.cust.ADDRESS.SALUTATION.SAL1, Model.salutationList)
        // @Html.DropDownList("Salutation", Model.salutationList)
    </div>
    <div class="editor-label">
    </div>
    <div class="editor-field">
        @Html.Label("Last name:")
        @Html.EditorFor(model => model.cust.ADDRESS.LASTNAME)
        @Html.ValidationMessageFor(model => model.cust.ADDRESS.LASTNAME)
    </div>
    <p>
        <input type="submit" value="Speichern" />
    </p>
}

Changing and saving last names works fine. But when saving the salutation it changes the SAL1 value in the SALUTATION entity to the one I've chosen in the DropdownListFor. What I want is to change the salutation_id inside the ADDRESS entity for my customer. Why isn't that working?

Another strange behavoior: When removing the marked line in my CustomerController, I can't even change and save the last name. Normally the constructor of the CustimerViewModel sets the customer. So why do I have to have the line of code for setting the customer inside my ViewModel? It's duplicated, but has to be there...

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗲 2024-12-26 10:56:37

您的列表中需要有选定的属性。

我可以向您展示我的工作示例:

public static IEnumerable<SelectListItem> GetCountries(short? selectedValue)
    {
        List<SelectListItem> _countries = new List<SelectListItem>();
        _countries.Add(new SelectListItem() { Text = "Select country...", Value = "0", Selected = selectedValue == 0 });
        foreach (var country in ObjectFactory.GetInstance<DataRepository>().GetCountries())
        {
            _countries.Add(new SelectListItem()
            {
                Text = country.Name,
                Value = country.ID.ToString(),
                Selected = selectedValue > 0 && selectedValue.Equals(country.ID)
            });
        }
        return _countries;
    }  

在控制器中,我将其存储到 viewbag 中:

ViewBag.Countries = CompanyModel.GetCountries(0);

在视图中:

@Html.DropDownListFor(m => m.CompanyModel.CountryId, (IEnumerable<SelectListItem>)ViewBag.Countries)

You need to have Selected property in your list.

I can show you my working example:

public static IEnumerable<SelectListItem> GetCountries(short? selectedValue)
    {
        List<SelectListItem> _countries = new List<SelectListItem>();
        _countries.Add(new SelectListItem() { Text = "Select country...", Value = "0", Selected = selectedValue == 0 });
        foreach (var country in ObjectFactory.GetInstance<DataRepository>().GetCountries())
        {
            _countries.Add(new SelectListItem()
            {
                Text = country.Name,
                Value = country.ID.ToString(),
                Selected = selectedValue > 0 && selectedValue.Equals(country.ID)
            });
        }
        return _countries;
    }  

In controller i store this into viewbag:

ViewBag.Countries = CompanyModel.GetCountries(0);

In view:

@Html.DropDownListFor(m => m.CompanyModel.CountryId, (IEnumerable<SelectListItem>)ViewBag.Countries)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文