Note that there are some explanatory texts on larger screens.

plurals
  1. POCompleting the modelstate after autocompletion
    primarykey
    data
    text
    <p>So here is the problem i'm running into.</p> <p>Originally, I was populating the recipient field with a select list of users that one would have friended first.</p> <p>That didn't seem user friendly enough to me. So, I started working on an autocomplete with jQuery. The autocomplete works like a charm.</p> <p>The issue i'm running into is when I send the message, my <code>modelstate</code> isn't valid as the Message. The recipient only has the username filled in.</p> <p>Should I get rid of the validation and manually fill all these fields? Or can I do something smarter with this issue?</p> <pre><code>&lt;div class="label"&gt;@Html.LabelFor(model =&gt; model.Receiver.UserName)&lt;/div&gt; &lt;div class="fullinput"&gt;@Html.TextBoxFor(model =&gt; model.Receiver.UserName)&lt;/div&gt;&lt;br /&gt; &lt;div class="label"&gt;@Html.LabelFor(model =&gt; model.Subject)&lt;/div&gt; &lt;div class="fullinput"&gt;@Html.TextBoxFor(model =&gt; model.Subject)&lt;/div&gt;&lt;br /&gt; &lt;div class="label"&gt;@Html.LabelFor(model =&gt; model.Content)&lt;/div&gt; &lt;div class="fullinput"&gt;@Html.TextAreaFor(model =&gt; model.Content)&lt;/div&gt;&lt;br /&gt; </code></pre> <p>That is the html I'm using.</p> <pre><code>$("#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 } })) } }); }, </code></pre> <p>This is the autocomplete call:</p> <pre><code>[HttpPost] public ActionResult Send(Message message) { User user = entities.Users.SingleOrDefault(u =&gt; 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); } </code></pre> <p>There I check for the validation, and it's always invalid which makes sense, I guess.</p> <pre><code>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; } } </code></pre> <p>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. </p> <p>I'm wondering if i can just skip checking modelstate in this case or if i should fix this in a nicer way.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload