How to pass data from viewbag to model in CSHTML
I'm having a hard time passing data to my model using viewbag.
Here is some example to enlighten you:
@model UCPBEscheatment.Models.aspnet_Membership
@{
ViewBag.Title = "AnswerForgotPassword";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
<em style="font-size:small">@Html.ValidationSummary(true, "Forgot Password was unsuccessful. Please correct the errors and try again.")</em>
<fieldset style="background-color:#f0f0f0">
<legend&g开发者_运维问答t; Forgot Password </legend>
<br />
• Second step: <em style="color:Teal"> Answer your secret question</em>
<br /><br />
<b> Secret Question:</b> @ViewBag.question
<br /><br />
<b> Secret Answer: </b>@Html.EditorFor(a => a.PasswordAnswer)
<br /><br />
@{
INPUT CODE HERE TO PASS A VALUE TO THE MODEL USING VIEWBAG LIKE:
ex.
@MODEL.USERID = VIEWBAG.USERID
}
<input type="submit" value="Submit" />
</fieldset>
}
It doesn't work that way. You can't assign to the model that is being returned in the post. I'd suggest adding the user's id to the form url and using the id on the url to find the user that needs the password secret validated.
@using (Html.BeginForm("validatepassword","account",new { id = ViewBag.UserID }))
{
...
}
Or better yet, set the user id on the model and use it from there instead of the ViewBag, but still using it in the url to post back.
精彩评论