且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-25 18:08:59

参考从MVC中获取数据从视图到控制器 [ ^ ]


hi


我们有很多方法可以将数据从视图传递到控制器,具体如下: -



1. ViewData

2. ViewBag

3. TempData

4.使用模型



以下是一些链接,你可以得到这个代码



http:// tutorial .techaltum.com / ViewData-in-MVC.html [ ^ ]



http://tutorial.techaltum.com/ViewBag-in-MVC.html [ ^ ]







http://tutorial.techaltum.com/UpdateModel-in-MVC.html [ ^ ]


您可以使用ViewModel来完成,例如如何将数据从控制器传递到视图。



假设你有一个像这样的视图模型



公共类ReportViewModel 
{
公共字符串名称{set; get;}
}



和你的GET动作,



公共ActionResult报告()
{
return View(new ReportViewModel());
}



你的观点必须强烈输入ReportViewModel



 @model ReportViewModel 
@using(Html.BeginForm())
{
报告NAme:@ Html.TextBoxFor(s => s.Name)
<输入类型=submitvalue =生成报告/>
}



和控制器中的HttpPost操作方法



 [HttpPost] 
公共ActionResult报告(ReportViewModel模型)
{
//检查model.Name属性值现在
//要做:返回
}



或者简单地说,你可以在没有POCO课程的情况下做到这一点(Viewmodels)



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



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

 [HttpPost] 
public ActionResult Report(string reportName)
{
//现在检查reportName参数值
//要做:返回
}





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



 @using(Html.BeginForm(Report,SomeOtherControllerName))
{
< input type =textname =reportName /&GT;
< input type =submit/>
}





参考:计算器


I want to get view data on controller

refer Getting Data From View to Controller in MVC[^]


hi
we have so many ways to pass data from view to controller and these are as follows:-

1. ViewData
2. ViewBag
3. TempData
4. using Model

following are some links where you can get the code for this

http://tutorial.techaltum.com/ViewData-in-MVC.html[^]

http://tutorial.techaltum.com/ViewBag-in-MVC.html[^]

and

http://tutorial.techaltum.com/UpdateModel-in-MVC.html[^]


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
}



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



Reference: ***