ASP.Net MVC 如何将数据从视图传递到控制器 [英] ASP.Net MVC How to pass data from view to controller

查看:50
本文介绍了ASP.Net MVC 如何将数据从视图传递到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 ASP.Net 完全陌生,我相信这是一个非常基本的问题我有一个视图,其中有一个生成报告的链接,但为了能够生成报告,我还必须要求用户提供合适的文本名称.

I am completely new to ASP.Net and I am sure this is a very basic question I have a View in which there is a link to generate report but to be able to generate report I must ask the user to provide a suitable text name as well.

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

So far I have been able to pass data from server to view using Models passed from my controller to view, but I am not sure how to Pass data from view to my controller.

在这种情况下,我只需要将字符串从视图传递给控制器​​.

I just need to pass a string from view to controller in this case.

任何带有示例的建议将不胜感激.

Any advice with example will be appreciated.

更新

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

I understand I have to post the data back to server but how does that realize in the form of razorhtml code and controller?

推荐答案

您可以使用 ViewModel 来实现,就像您将数据从控制器传递到视图一样.

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;}
}

在您的 GET 操作中,

and in your GET Action,

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

并且您的视图必须强类型为 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" />
}

以及控制器中的 HttpPost 操作方法

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 简单地说,您可以在没有 POCO 类(视图模型)的情况下做到这一点

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

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

并且在您的 HttpPost 操作中,使用与文本框名称同名的参数.

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
}

根据评论

如果你想发布到另一个控制器,你可以使用这个BeginForm 方法的重载.

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" />
}

从动作方法传递数据到视图?

您可以使用相同的视图模型,只需在 GET 操作方法中设置属性值

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);
}

在你看来

@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" />
}

这篇关于ASP.Net MVC 如何将数据从视图传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆