0

客户

public class ClientModelData
{
    public int ClientID { get; set; }
    public string ClientName { get; set; }
    public string Phone { get; set; }
    public string Address { get; set; }
    public IList<int> LocationID { get; set; }
}

控制器

[HttpPost]
    public ActionResult AddClient(ClientModelData client)
    {
        Client _client = new Client()
        {
            //ClientCode=client.ClientCode,
            ClientName = client.ClientName,
            Phone = client.Phone,
            Ext = client.Ext,
            Address = client.Address,
            CreatedBy = 1,
            CreatedDate = DateTime.Now,
            ModifiedBy = 1,
            ModifiedDate = DateTime.Now,
            ModifiedIP = Request.ServerVariables["REMOTE_ADDR"]
        };
        db.Clients.Add(_client);
        try
        {
            db.SaveChanges();
        }
        catch (DbEntityValidationException dbEx)
        {
            string error = string.Empty;
            foreach (var validationErrors in dbEx.EntityValidationErrors)
                foreach (var validationError in validationErrors.ValidationErrors)
                    error += string.Format("Property: {0} Error: {1}", validationError.PropertyName, 
                        validationError.ErrorMessage);

            return Json(new
            {
                status = false,
                message = "Unable to update information for selected client, please contact administrator. For detail <a class='errordetail'>click here</a>",
                exception = error
            });
        }
        return Json(_client);
    }

我有一个复选框列表:

<ul class="locations">
                                @foreach (var item in Model.Locations)
                                {
                                    <li>
                                        <label>
                                            <input type="checkbox" id="chkLocations" name="chkLocations" 
                                            value="@item.LocationID" />
                                            @item.LocationTitle</label>
                                    </li>
                                }
                            </ul>

** Json 对象包含以下值:**

var allVals = [];
            $('#chkLocations:checked').each(function () {
                allVals.push($(this).val());
            });
            var dataPacket = {
                ClientName: $("#txtClientName").val(),
                Phone: $("#txtPhone").val(),
                Ext: $("#txtExt").val(),
                Address: $("#txtAddress").val(),
                LocationID: allVals
            };

现在问题出现了,当我给出复选框:选中的项目时,它不会填充 ModelData Entity Location属性

图片:

http://tinypic.com/r/eapem9/6

问题出现 POSTING 表单...JQuery AJAX

图片:

http://tinypic.com/r/4l0bqr/6

4

1 回答 1

0

不幸的是,您还没有向我们展示您是如何调用控制器操作的。您可以尝试将它们作为 JSON 请求发送,该请求允许您将任意复杂的对象发布到控制器操作:

var allVals = [];
$('#chkLocations:checked').each(function () {
    allVals.push($(this).val());
});
var dataPacket = {
    ClientName: $("#txtClientName").val(),
    Phone: $("#txtPhone").val(),
    Ext: $("#txtExt").val(),
    Address: $("#txtAddress").val(),
    LocationID: allVals
};

接着:

$.ajax({
    url: '@Url.Action("AddClient", "SomeController")',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify({ client: dataPacket }),
    success: function(result) {
        // ... handle the result
    }
});

JSON.stringify请注意现代浏览器中原生内置的方法的用法,以将请求作为 JSON 字符串发送。我们还将请求内容类型标头设置为application/json,以便应用程序使用正确的 JSON 值提供程序并将此请求解析为您的模型。

于 2012-10-01T16:01:23.803 回答