Asp.net MVC 中 一個HttpPost後 View中的資料全部沒有了 有沒有什麼方法可以設定儲存它的資料狀態不丟失在
- 2022-09-13
你可以用DataBinder來儲存需要傳遞的資料, 這樣在POST之後表單裡的資訊還是在的
例如,有個表單要提交姓名和電郵
1。 ViewModel是
Public class UserModel{
public Name{get; set;}
public Email{get; set;}
}
2。 Controller Action是, 假設你Get 和 Post 都是用一個View, 模擬asp。net的postback
public ActionResult Profile(){
// 因為是共享一個VIEW所以這裡返回一個空的view model防止報錯
Return View(new UserModel());
}
[HttpPost]
public ActionResult Profile(UserModel model){
//在提交表單的時候MVC會自動填充model裡相應的欄位,這就是DataBinder
。。。。。
//在這裡返回model到View, 再把model裡的值付給表單裡的姓名和電郵就好了
Return View(model);
}
3。 View
<% using (Html。BeginForm()){ %>
姓名: <%= Html。TextBox(“Name”, Model。Name) %>
電郵: <%= Html。TextBox(“Email”, Model。Email) %>
<% } %>
html。actionlink(“連結文字”,“action”,new {引數1=“ddc”,引數2=“333”})
這麼些就行
上一篇:為什麼我家的電視螢幕總是抖動
下一篇:PS如何調成這種色調?