如何将数据从视图传递到控制器

我对 ASP.Net 完全不熟悉,我确信这是一个非常基本的问题 我有一个视图,其中有一个链接生成报告,但能够生成报告,我必须要求用户提供一个合适的文本名称以及。

到目前为止,我已经能够使用从控制器传递到视图的模型将数据从服务器传递到视图,但是我不确定如何将数据从视图传递到控制器。

在这种情况下,我只需要将一个字符串从 view 传递到 controller。

任何有榜样的建议都会受到欢迎。

更新

我知道我必须将数据发送回服务器,但是如何以 razorhtml 代码和控制器的形式实现这一点呢?

451115 次浏览
<form action="myController/myAction" method="POST">
<input type="text" name="valueINeed" />
<input type="submit" value="View Report" />
</form>

controller:

[HttpPost]
public ActionResult myAction(string valueINeed)
{
//....
}

You can do it with ViewModels like how you passed data from your controller to view.

Assume you have a viewmodel like this

public class ReportViewModel
{
public string Name { set;get;}
}

and in your GET Action,

public ActionResult Report()
{
return View(new ReportViewModel());
}

and your view must be strongly typed to ReportViewModel

@model ReportViewModel
@using(Html.BeginForm())
{
Report NAme : @Html.TextBoxFor(s=>s.Name)
<input type="submit" value="Generate report" />
}

and in your HttpPost action method in your controller

[HttpPost]
public ActionResult Report(ReportViewModel model)
{
//check for model.Name property value now
//to do : Return something
}

OR Simply, you can do this without the POCO classes (Viewmodels)

@using(Html.BeginForm())
{
<input type="text" name="reportName" />
<input type="submit" />
}

and in your HttpPost action, use a parameter with same name as the textbox name.

[HttpPost]
public ActionResult Report(string reportName)
{
//check for reportName parameter value now
//to do : Return something
}

EDIT : As per the comment

If you want to post to another controller, you may use this overload of the BeginForm method.

@using(Html.BeginForm("Report","SomeOtherControllerName"))
{
<input type="text" name="reportName" />
<input type="submit" />
}

Passing data from action method to view ?

You can use the same view model, simply set the property values in your GET action method

public ActionResult Report()
{
var vm = new ReportViewModel();
vm.Name="SuperManReport";
return View(vm);
}

and in your view

@model ReportViewModel
<h2>@Model.Name</h2>
<p>Can have input field with value set in action method</p>
@using(Html.BeginForm())
{
@Html.TextBoxFor(s=>s.Name)
<input type="submit" />
}

In case you don't want/need to post:

@Html.ActionLink("link caption", "actionName", new { Model.Page })  // view's controller
@Html.ActionLink("link caption", "actionName", "controllerName", new { reportID = 1 }, null);


[HttpGet]
public ActionResult actionName(int reportID)
{

Note that the reportID in the new {} part matches reportID in the action parameters, you can add any number of parameters this way, but any more than 2 or 3 (some will argue always) you should be passing a model via a POST (as per other answer)

Edit: Added null for correct overload as pointed out in comments. There's a number of overloads and if you specify both action+controller, then you need both routeValues and htmlAttributes. Without the controller (just caption+action), only routeValues are needed but may be best practice to always specify both.