2013-01-23 44 views
-1

我想設置值到ViewBag,像波紋管如何使用ViewBag值設置模型屬性?

public ActionResult Register(Guid accountId) 
{ 
      ViewBag.AccountId = accountId; 

      return View("Register", "Account"); 
    } 

現在內註冊查看,我可以設置,如波紋管

@Html.HiddenFor(m => m.AccountId, new { id = "hdaccountid", value = ViewBag.AccountId }) 

我想更新與ViewBag價值模型屬性。所以當我想在控制器內更新模型時,所以model屬性包含適當的值。

我不想使用ModelView。請建議。

回答

1

如果我的理解......

,如果你不通過模型來查看您不能使用HiddenFor

查看

@Html.Hidden("accountId", ViewBag.AccountId) 
@Html.Hidden("id", "hdaccountid") 

控制器

// pass parameter to view 
[HttpGet] 
public ActionResult Register(Guid accountId) 
{ 
    ViewBag.AccountId = accountId; 

    return View(); 
    //return View("Register", "Account"); 
} 

// post parameter from view 
[HttpPost] 
public ActionResult Register(Guid accountId, string id) 
{ 
    // Get model from DB and change it 
    // model.accountId = accountId; 
    ... 
} 
+0

如果我已經'MoveToRegister(GUID帳戶ID)'設置ACCOUNTID爲u描述爲隱藏,則另一個'寄存器(的usermodel模型)的圖內'從這個更新數據庫? –

+0

如果你想從視圖中獲取模型,你應該傳遞模型來查看。 –

+0

@AliRızaAdıyahşi:如果我想,隱藏在控制器中的價值,那麼我怎麼能得到。其實我有兩個ActionResult - >從它通過accountid到一個視圖。然後輸入一些值 - >提交表單 - >調用另一個操作結果並將數據保存到數據庫中,包括隱藏的字段值。 –