在使用MVC向Modal中的ajax函数发送DropDownList值时,出现空值。

问题描述 投票:0回答:1

我正在使用EntityFramework创建一个新的MVC WebSite,在我的_Layout.cshtml中,我正在调用一个PartialView,这是一个Modal,用于在一个特定的表上插入一条记录。

这个部分视图包含以下DDL。

<div class="form-group">
   @Html.LabelFor(model => model.Currency, htmlAttributes: new { @class = "control-label col-md-2" })
       <div class="col-md-10">
           <select class="form-control" id="CurrencyId" name="CurrencyId">
                <option value="">Select currency...</option>
           </select>
        </div>
</div>

它使用了一个自动生成的模型

using System;
using System.Collections.Generic;

public partial class Property
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Property()
    {
        this.Attachments = new HashSet<Attachment>();
        this.BankAccountCorrienteProperties = new HashSet<BankAccountCorrienteProperty>();
        this.Contracts = new HashSet<Contract>();
        this.Photos = new HashSet<Photo>();
        this.Renters = new HashSet<Renter>();
    }

    public int IdProperty { get; set; }
    public Nullable<int> IdOwner { get; set; }
    public Nullable<int> PropertyType { get; set; }
    public string StreetName { get; set; }
    public Nullable<decimal> SquareMetersSize { get; set; }
    public Nullable<bool> Garage { get; set; }
    public Nullable<int> PropertyStatus { get; set; }
    public string ConstructionDate { get; set; }
    public Nullable<short> RentOrSell { get; set; }
    public Nullable<int> Currency { get; set; }
    public Nullable<decimal> Price { get; set; }
    public string Notes { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Attachment> Attachments { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<BankAccountCorrienteProperty> BankAccountCorrienteProperties { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Contract> Contracts { get; set; }
    public virtual Currency Currency1 { get; set; }
    public virtual Owner Owner { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Photo> Photos { get; set; }
    public virtual PropertyType PropertyType1 { get; set; }
    public virtual PropertyStatu PropertyStatu { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Renter> Renters { get; set; }
}

为了将数据加载到下拉菜单中,我不得不使用Ajax,如下所示。

getDdlInfo("../Currency/ListCurrencies", function (result) {
$.each(result.data, function (key, item) {
    $("#CurrencyId").append($('<option></option>').val(item.CurrencyId).text(item.CurrencyName));
});

function getDdlInfo(path, callBackFunct) {
$.ajax({
    type: "GET",
    url: path,
    success: function (result) {
        return callBackFunct(result);
    },
    error: function (data) {
    }
});

然后将值发送到控制器,其中有一个HTTP Post来发送数据到数据库。

function createProperty() {
$.ajax({
    url: '../Property/CreateProperty',
    type: 'POST',
    cache: false,
    async: true,
    data: $('form').serialize(),
    success: function (result) {
        //TODO: Modal de propiedad cargada
    }
});

我的问题是:1) 当... ... createProperty() 进入控制器,所有数据都在那里,除了下拉菜单。不管我是否选择了一个值,它到控制器总是空的,而且...2)这是正确的方法吗?我是不是遗漏了什么?我试过用Razor DropDownList,但我找不到正确的方法。

谢谢你。

javascript asp.net-mvc razor asp.net-ajax
1个回答
0
投票

在你的模型中,这个属性被称为 Currency,所以你的输入的名称需要与之匹配才能成功地进行模型绑定。将输入的名称改为 Currency 应解决。

<select class="form-control" id="CurrencyId" name="Currency">

© www.soinside.com 2019 - 2024. All rights reserved.