View data from view to controller
Is there an easy开发者_如何学JAVA way to pass an object from my view to controller?
I tried ViewData["newPerson"]
but that didn't work.
Session["newPerson"]
works but are there other ways that are more recommended?
Usually you'd receive a model as the parameter. You'd have to have form fields that map to the model's properties.
public class PersonViewModel
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
...
}
Then in your view:
@model PersonViewModel
@using (Html.BeginForm())
{
<div class="editor-label">@Html.LabelFor( model => model.FirstName )</div>
<div class="editor-field">@Html.EditorFor( model => model.FirstName )</div>
<div class="editor-label">@Html.LabelFor( model => model.LastName )</div>
<div class="editor-field">@Html.EditorFor( model => model.LastName )</div>
...
}
Then in the actions corresponding to the view send and receive the model
[HttpGet]
public ActionResult CreatePerson()
{
return View( new Person() );
}
[HttpPost]
public ActionResult CreatePerson( PersonViewModel person )
{
var person = ...create and persist a Person entity based on the view model....
return Redirect("details", new { id = person.id } );
}
You shouldn't want to pass an object from your view to your controller, outside of calling a controller's action. If you're trying to pass a parameter to your controller action, you can add in a route value.
For example this will pass the value 3 as the id parameter into your action (using the route logic)
@Html.ActionLink("Home", "Details", new { id = 3 });
Are you implementing a form? Your best bet would be to create a new action on your controller that takes in your object. Your controller would have a method like this:
public ActionResult MyAction(Person person)
{
// your code
}
Or if you want it to respond to posts:
[HttpPost]
public ActionResult MyAction(Person person)
{
// your code
}
Then your view would call your controller action via a link or a form submit. One example for a link:
@Html.ActionLink("MyContoller", "MyAction", newPerson);
My impression is that you declare your handler to take a parameter of type FormCollection. That can be indexed by the text of your variable to retrieve the value. Watch for typos--attempting to retrieve a value that doesn't exist simply returns a blank.
Based upon the accepted answer, my answer isn't what you are looking for, but based upon Google (at time of writing), this is the SO answer most responsive to actually passing data from a View to a Controller.
If you set the value on WebViewPage<T>.ViewContext.ViewData
, it can be accessed from the controller. We are using this technique to set an email subject in a view that we render to be the body of an HTML email. (This approach allows us to put the definition of the subject together with the body of the email and to access this subject from the controller where we actually send the email.)
/// <summary>
/// Sends a system email containing a rendered view.
/// </summary>
public static void SendSystemEmailOfView(this Controller controller, string to, [AspMvcView] string viewName, object model, ViewDataDictionary viewData = null)
{
if (viewData == null)
{
viewData = new ViewDataDictionary();
}
var body = controller.Render(viewName, model, viewData);
object subject;
if (viewData.TryGetValue(WebViewPageExtensions.ViewDataKey_EmailSubject, out subject))
{
subject = subject as string;
}
if (subject == null)
{
subject = DefaultEmailSubject;
}
SendEmail(to, (string)subject, body, isHtml: true);
}
Notes: 1) AspMvcViewAttribute
is a Resharper annotation that you can omit if you don't use Resharper. 2) controller.Render
and SendEmail
are external methods (there are many SO answers about how to send emails and how to manually render a view to a string from within a controller.)
Our view looks like this:
@model MyViewModel
@{
this.EmailSubject("Subject Here");
}
<p>
Dear Web Forms,
</p>
<p>
I'm leaving you for MVC...it's just so much more...DRY.
</p>
and the .EmailSubject
is like this:
public const string ViewDataKey_EmailSubject = "MyAppName_EmailSubject";
/// <summary>
/// Sets an email subject in <paramref name="page"/>'s <see cref="WebViewPage{T}.ViewBag"/>. Useful for retrieving the subject from controller.
/// </summary>
public static void EmailSubject<T>(this WebViewPage<T> page, string subject)
{
// Must set value on ViewContext's ViewData if we want to access it after view renders; page.ViewData setter copies the ViewDataDictionary.
page.ViewContext.ViewData[ViewDataKey_EmailSubject] = subject;
}
精彩评论