简体   繁体   English

如何将ListBoxFor值保存到MVC5中的数据库

[英]How to save ListBoxFor Values to database in MVC5

I want to save all listBoxFor values to the Database... when posting the form i have this error saying Object not Set to an Instant of an Object, Can anyone help me to figure out what the problem is? 我想将所有listBoxFor值保存到数据库...在发布表单时出现此错误,提示“对象未设置为对象的瞬时值”,有人可以帮助我找出问题所在吗?

This is all the code 这是所有的代码

This is the listbox code that contain all the values: 这是包含所有值的列表框代码:

@Html.ListBoxFor(model => model.SelectedRecipientId, new MultiSelectList(Model.Recipients, "ID", "RecipientNumbers"), new { id = "recipientList", style = "width: 250px; height: 160px;", name = "recipientList" })

This is my View model 这是我的View模型

public class SendViewModel
{
    public SendViewModel()
    {
        ScheduledDate = DateTime.Now;
    }

    public int Id { get; set; }
    public string Message { get; set; }
    public DateTime ScheduledDate { get; set; }

    public IEnumerable<SelectListItem> Times { get; set; }
    public string[] SelectedRecipientId { get; set; }

    [Required]
    public IEnumerable<SelectListItem> Recipients { get; set; }
}

This is the model that I am trying to write to 这是我要写的模型

public class SentMessage
{
    public int Id { get; set; }
    public string Message { get; set; }
    public string SenderId { get; set; }
    public DateTime SendDate { get; set; }
    public int RecipientCount { get; set; }
    ...

    public virtual ICollection<MessageRecipient> Recipients { get; set; }
}

This is my Post Controller 这是我的后控制器

if (ModelState.IsValid)
{
    if (model.ScheduledDate == DateTime.Now)
    {
        _messageService.SendMessage(User, SenderId, model.Message, model.SelectedRecipientId);

        return Redirect("/UserAccount/SendMessageConfirmation");
    }
}

And lastly this is the message service parameter collection 最后是消息服务参数集合

int SendMessage(int userId, string senderId, string message, IEnumerable<string> recipients);

This is the Signature that save all the data from the view to the database and the IEnumerable<string> recipients is the parameter responsibl for getting all the values from the listbox. 这是将所有数据从视图保存到数据库的签名,而IEnumerable<string> recipients是负责从列表框中获取所有值的参数。

How do I go about it so that it will be able to save to the database. 我该怎么做,以便它将能够保存到数据库。

This is the Error stack Trace 这是错误堆栈跟踪

Server Error in '/' Application.

Object reference not set to an instance of an object. 你调用的对象是空的。

Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

Source Error: 源错误:

Line 68: Line 69: Recipients Line 70: @Html.ListBoxFor(model => model.SelectedRecipientId, new MultiSelectList(Model.Recipients, "ID", "RecipientNumbers"), new { id = "recipientList", style = "width: 250px; height: 160px;", name = "SelectedRecipientId" }) Line 71: Line 72: 行68:行69: 收件人行70:@ Html.ListBoxFor(model => model.SelectedRecipientId,新的MultiSelectList(Model.Recipients,“ ID”,“ RecipientNumbers”),新的{id =“ recipientList”,样式=“宽度:250px;高度:160px;“,名称=” SelectedRecipientId“})第71行:第72行:

Source File: c:\\Development\\Projects\\SMSXChange\\SmsXchange\\XChange.MVC\\Views\\Message\\Send.cshtml Line: 70 源文件:c:\\ Development \\ Projects \\ SMSXChange \\ SmsXchange \\ XChange.MVC \\ Views \\ Message \\ Send.cshtml行:70

Stack Trace: 堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an object.] ASP._Page_Views_Message_Send_cshtml [NullReferenceException:对象引用未设置为对象的实例。] ASP._Page_Views_Message_Send_cshtml

Thanks for the error details, 感谢您提供错误详细信息,

Although the thing i mentioned in my comment is correct that both " HTML Element Name " and " Model Property Name " should be similar to post the value but here the problem is something different for the error you are getting. 尽管我在评论中提到的事情是正确的,“ HTML Element Name ”和“ Model Property Name ”都应该与发布值相似,但此处的问题与您得到的错误有所不同。

Make sure that you are filling your Model.Recipients property correctly before rendering the view. 在渲染视图之前,请确保正确填充了Model.Recipients属性。 Your action from where you are calling the view should assign correct value for Model.Recipients property. 您从调用视图的位置开始的操作应为Model.Recipients属性分配正确的值。 Your property is null hence it is throwing an object reference error. 您的属性为null,因此会引发对象引用错误。

In fact the error should come on initial load of the form and not after posting it. 实际上,该错误应该出现在表单的初始加载上,而不是发布之后。 If still it is coming after posting the form then there is following possibility. 如果仍然在发布表单后到来,则存在以下可能性。 As you have added ModelState.IsValid check it seems this is not getting satisfied and there is some validation error which is causing the Post action method to render same view again (Provider you have are not doing anything else in else part) and when the view gets loaded again it doesn't have the select list filled as you haven't added that code in post method. 当您添加ModelState.IsValid检查后,这似乎并不令人满意,并且存在一些验证错误,这导致Post操作方法再次呈现相同的视图(提供者,您在其他部分没有执行其他任何操作)再次加载,因为您没有在post方法中添加该代码,所以没有填写选择列表。 Hence the possible solution would be to fill the Model.Recipients again on POST action if there is any validation error. 因此,可能的解决方案是,如果存在任何验证错误,则在POST操作中再次填充Model.Recipients I hope you got my point 我希望你明白我的意思

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM