簡體   English   中英

在ASP.NET MVC中使用模型保留屬性(使用Binder?)

[英]Persisting Properties in Models in ASP.NET MVC (with Binder?)

給出以下代碼:

楷模

class Log
{
   ...
   Ticket Ticket { get; set; }
   string Message { get; set; }
}
class Ticket
{
   ...
   Importance Importance { get; set; }
   string Name { get; set; }
   ...
}

視圖

<%@ Language="C#" Inherits="System.Web.Mvc.ViewPage<Models.Log>" %>
...
<%= Html.DisplayFor(l => l.Ticket.Name) %>
<%= Html.EditorFor(l => l.Message) %>
<%= Html.EditorFor(l => l.Ticket.Importance) %>
...

控制器動作

[HttpGet]
public ActionResult Update(int id)
{
    Ticket t = _tickets.Get(id);
    return View(new Log { Ticket = t });
}

[HttpPost]
public ActionResult Update(Log l)
{
   // My problem is here:
    l.Ticket.Name; // This is null
    l.Ticket.Importance; // while this one is still set
}

有什么方法可以將工Ticket保留在通過的Log中?

名稱將輸出為純文本顯示,這意味着不會有變量作為帖子的一部分傳遞回頁面,因為它不是表單的一部分。 解決方法是將其放在隱藏字段中或作為Update方法的一部分查找模型,然后在檢索到的項目上調用UpdateModel。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM