我有以下实体模型:
public class AssetLabel
{
public string QRCode { get; set; }
public string asset { get; set; }
public virtual IEnumerable<Conversation> Conversations { get; set; }
}
public class Conversation
{
public int ID { get; set; }
public virtual AssetLabel AssetLabel{ get; set; }
public string FinderName { get; set; }
public string FinderMobile { get; set; }
public string FinderEmail { get; set; }
public ConversationStatus Status{ get; set; }
public IEnumerable<ConversationMessage> Messages { get; set; }
}
public class ConversationMessage
{
public int ID { get; set; }
public DateTime MessageDateTime { get; set; }
public bool IsFinderMessage { get; set; }
public virtual Conversation Conversation { get; set; }
}
public enum ConversationStatus { open, closed };
public class FinderViewModel : Conversation
{/*used in Controllers->Found*/
}
我的 MVC 应用程序将提示QRCode
一个 POST 请求。然后我验证此代码是否存在于数据库中AssetLabel
,并且满足其他一些服务器端逻辑。然后我需要请求用户联系方式来创建新 Conversation
记录。目前我有一个 GET 到一个控制器操作,它返回第一个表单来捕获代码。如果这是有效的,那么我创建一个新 的,用对象FinderViewModel
填充并返回一个视图以使用 vm 并显示,和的字段。我的问题是,虽然作为 的一部分被传递给视图,但我可以显示来自;的字段。图形对象不会在 POST 中传回。我知道我可以修改AssetLabel
QRCode
Name
Mobile
Email
AssetLabel
FinderViewModel
AssetLabel
AssetLabel
FinderViewModel
因此它将Conversation
as 一个属性设置QRCode
为一个单独的属性,该属性可能是表单中的隐藏字段,然后重新找到 theAssetLabel
作为第二个表单处理的一部分,但这感觉像是很多工作因为我已经验证过一次以达到创建第二种形式的目的(这就是我离开 PHP MVC 框架的原因)。
第一个问题是如何?,第二个问题是我是否以错误的方式接近这种设计模式。是否有更 .NETty 的方式通过多种形式保存数据?在我学习的这一点上,我真的不想将信息存储在 cookie 中或使用 ajax。
作为参考,第 1 形式 POST、第 2 视图和第 2 形式 POST 的其余代码如下所示(已简化以消除不相关的逻辑)。
public class FoundController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Found
public ActionResult Index()
{
AssetLabel lbl = new AssetLabel();
return View(lbl);
}
[HttpPost]
public ActionResult Index(string QRCode)
{
if (QRCode=="")
{
return Content("no value entered");
}
else
{
/*check to see if code is in database*/
AssetLabel lbl = db.AssetLables.FirstOrDefault(q =>q.QRCode==QRCode);
if (lbl != null)
{
var vm = new FinderViewModel();
vm.AssetLabel = lbl;
vm.Status = ConversationStatus.open;
return View("FinderDetails", vm);
}
else
{/*Label ID is not in the database*/
return Content("Label Not Found");
}
}
}
[HttpPost]
public ActionResult ProcessFinder(FinderViewModel vm)
{
/*
THIS IS WHERE I AM STUCK! - vm.AssetLabel == NULL even though it
was passed to the view with a fully populated object
*/
return Content(vm.AssetLabel.QRCode.ToString());
//return Content("Finder Details posted!");
}
FinderView.cshtml
@model GMSB.ViewModels.FinderViewModel
@{
ViewBag.Title = "TEST FINDER";
}
<h2>FinderDetails</h2>
@using (Html.BeginForm("ProcessFinder","Found",FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Finder Details</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
@Html.HiddenFor(model => model.AssetLabel)
<div class="form-group">
@Html.LabelFor(model => model.FinderName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FinderName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FinderName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FinderMobile, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FinderMobile, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FinderMobile, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FinderEmail, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FinderEmail, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FinderEmail, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
AssetLabel 的渲染 HTML 片段
<input id="AssetLabel" name="AssetLabel" type="hidden"
value="System.Data.Entity.DynamicProxies.AssetLabel_32653C4084FF0CBCFDBE520EA1FC5FE4F22B6D9CD6D5A87E7F1B7A198A59DBB3"
/>