Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8592409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:44:40+00:00 2026-06-11T23:44:40+00:00

So here is the problem i’m running into. Originally, I was populating the recipient

  • 0

So here is the problem i’m running into.

Originally, I was populating the recipient field with a select list of users that one would have friended first.

That didn’t seem user friendly enough to me. So, I started working on an autocomplete with jQuery. The autocomplete works like a charm.

The issue i’m running into is when I send the message, my modelstate isn’t valid as the Message. The recipient only has the username filled in.

Should I get rid of the validation and manually fill all these fields?
Or can I do something smarter with this issue?

<div class="label">@Html.LabelFor(model => model.Receiver.UserName)</div>
<div class="fullinput">@Html.TextBoxFor(model => model.Receiver.UserName)</div><br />

<div class="label">@Html.LabelFor(model => model.Subject)</div>
<div class="fullinput">@Html.TextBoxFor(model => model.Subject)</div><br />

<div class="label">@Html.LabelFor(model => model.Content)</div>
<div class="fullinput">@Html.TextAreaFor(model => model.Content)</div><br />

That is the html I’m using.

$("#Receiver_UserName").autocomplete({ 
            source: function (request, response) {
                $.ajax({
                    url: "/Function/FindUsers", // Or your controller
                    dataType: "json",
                    data: { usr: request.term },
                    success: function (data) {
                        // Returned data follows the Spellword model
                        response($.map(data, function (item) {
                            return {
                                id: item.Id,
                                label: item.UserName,
                                value: item.UserName
                            }
                        }))
                    }
                });
            },

This is the autocomplete call:

[HttpPost]
    public ActionResult Send(Message message)
    {
        User user = entities.Users.SingleOrDefault(u => u.UserName.Equals(User.Identity.Name));
        //ViewData["Friends"] = new SelectList(user.Friends, "Id", "Name");

        if (ModelState.IsValid)
        {
            entities.Messages.Add(message);
            entities.SaveChanges();

            return RedirectToAction("Index");
        }
        return View(message);
    }

There I check for the validation, and it’s always invalid which makes sense, I guess.

public class Message
{
    [Key]
    public int Id { get; set; }

    [Required(ErrorMessage = "Subject is required")]
    [Display(Name = "Subject")]
    public string Subject { get; set; }

    [Required(ErrorMessage = "Message is required")]
    [Display(Name = "Message")]
    public string Content { get; set; }

    [Required]
    [Display(Name = "Date")]
    public DateTime Created { get; set; }

    public Boolean Read { get; set; }

    [Required(ErrorMessage = "Can't create a message without a user")]
    public int SenderId { get; set; }
    public virtual User Sender { get; set; }

    [Required(ErrorMessage = "Please pick a recipient")]
    public int ReceiverId { get; set; }
    public virtual User Receiver { get; set; }
}

And finally the Message Model.
Obviously i don’t have fields for all of these so this is why the modelstate is crying at me.

I’m wondering if i can just skip checking modelstate in this case or if i should fix this in a nicer way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:44:41+00:00Added an answer on June 11, 2026 at 11:44 pm

    I Solved the issue like this

    [HttpPost]
    public ActionResult Send(Message message)
    {
        User user = entities.Users.SingleOrDefault(u => u.UserName.Equals(User.Identity.Name));
        User recipient = entities.Users.SingleOrDefault(u => u.UserName.Equals(message.Receiver.UserName));
        ModelState.Clear();
    
        if (recipient != null && user != null)
        {
            message.Sender = user;
            message.SenderId = user.Id;
            message.Receiver = recipient;
            message.ReceiverId = recipient.Id;
            message.Created = DateTime.Now;
    
            entities.Messages.Add(message);
            entities.SaveChanges();
    
            return RedirectToAction("Index");
        }
        else
        {
            return View(message);
        }
    }
    

    But i’m not very happy with the solution as it circumvents my entire validation process

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem here is that I would like to pass an object to a
date here my problem: String datetime = 2012-03-24 23:20:51; I know that that string
here my problem, i'm using asp.net for my site, i get a list of
Unusual problem here: I have an app that uses a text file which contains
Small problem here with an MVC app that I'm not sure how to figure
I am having problem here with bitmaps..I want to remove the black background that
I have a problem here..... I have a List of ProcessStartInfo as List<ProcessStartInfo> I
I've got a problem here with an MSI deployment that I'm working on (using
Odd problem here. Working with an existing system that uses TinyMCE as it's text
The problem here is how to add an NSPopUpButton in Xcode 4's Interface Builder

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.