我正在使用Ajax并渲染部分视图。数据正好传递到控制器中。没有什么是空的。但是由于某些原因,在我的部分视图中,OwnerUser和AssignToUser为空,但是小时和正文正在使用正确的信息。我在我的项目的另一部分有相同的实现,它工作正常,即使用户进来。我无法弄清楚为什么在这个部分视图中ApplicationUser数据为空。
[HttpPost]
public ActionResult ChatAjax([Bind(Include = "Body")] Chat chat, string ChatData)
{
chat.Created = DateTimeOffset.Now;
var user = User.Identity.GetUserId();
chat.OwnerUserId = user;
chat.AssignToUserId = ChatData;
db.Chats.Add(chat);
db.SaveChanges();
if (Request.IsAjaxRequest())
{
return PartialView("_Chats", chat);
}
return RedirectToAction("LandingPage", "Admin");
}
这是我的部分视图:
@model BugTracker.Models.Chat
@{
var day = DateTimeOffset.Now.Offset.Hours;
}
<div class="direct-chat-msg">
<div class="direct-chat-info clearfix">
<span class="direct-chat-name pull-left">
@Model.OwnerUser.FirstName</span>
<span class="direct-chat-timestamp pull-right">
@Model.Created.AddHours(day)
</span>
<br />
<span class="direct-chat-timestamp pull-right">Sent to
@Model.AssignToUser.DisplayName
</span>
</div>
<!-- /.direct-chat-info -->
<div class="direct-chat-text" style="background:lightblue">
@Model.Body
</div>
<!-- /.direct-chat-text -->
</div>
这是我的聊天模型
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace BugTracker.Models
{
public class Chat
{
public int Id { get; set; }
public string Body { get; set; }
public DateTimeOffset Created { get; set; }
[Display(Name = "Owner")]
public string OwnerUserId { get; set; }
[Display(Name = "Assign")]
public string AssignToUserId { get; set; }
public virtual ApplicationUser OwnerUser { get; set; }
public virtual ApplicationUser AssignToUser { get; set; }
}
}