次のエンティティ モデルがあります。
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
1 つのプロパティとして取得し、フォームの非表示フィールドになる可能性のある別のプロパティとして設定し、2 番目のフォームの処理の一部としてQRCode
を再検索しますが、これを確認するAssetLabel
のは大変な作業のように感じます2 番目のフォームを作成するポイントに到達するために、既に一度検証したためです (これが、PHP MVC フレームワークから離れようとしている理由です)。
最初の質問はどのように?、2 番目の質問は、この設計パターンへのアプローチが間違っているかどうかです。複数のフォームを介してデータを永続化するための、より .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"
/>