ASP.Net MVC How to pass data from view to controller
>>To viewModels that passed data from controller to view.
public class ReportViewModel { public sring Name {get;set;} }
and in your Get Action,
public actionresult report(){ return view(new reportviewmodel()); }
and view must be strongly typed to reportviewmodel
@model ReportViewModel
@using(Html.BeginForm())
{ Name:@Html.TextBoxFor(s=>s.name)
<input type=”submit” value=”Generate report” /> }
HttpPost action method
[HttpPost]
public actionresult report(reportviewmodel model) { }
>>or simply we can do this without POCO Classes(ViewModels)
@using(Html.BeginForm())
{ <input type=”text” name=”reportName” />
<input type=”submit” /> }
and in 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 }
—–through Javascript
public void GetExcel(string FileName)
{
// DataTable dt = (DataTable)Session[“ExcelData”];
var DtReport = Session[“ExcelData”] asDataTable;
MemoryStream stream = null;
string[] ExcelArray = null;
switch (FileName){}
}
—In view
@model UIModel.
ProjectCostViewModel
@{ViewBag.Title =“Project Cost Report”;
Layout =“~/Views/Shared/_Layout.cshtml”;
}
scripttype=”text/javascript”>
function getTextBoxContent() {
if ($(“#TxtRounds”).val() > “0”) {
var FileName = “ProjectCostTotalRound”;
//window.location = “/Report/GetExcel?FileName=” + FileName + “&Round=” + $(“#TxtRounds”).val();
window.location = appPath+
“/Report/GetExcel?FileName=” + FileName + “&Round=” + $(“#TxtRounds”).val();
}
else {
alert(
“Please input at least one round free”);
}
}
script>
@ using (Html.BeginForm(“ProjectCostList”, “Report”, FormMethod.Post, new { @class = “form-horizontal”, role = “form” }))
{
<
divid=”divTCostReport”>
@Html.TextBox(
“TxtRounds”, (string)ViewBag.TxtRounds, new { @onkeypress = “return OnlyNumeric(event);” })
<br/>
<ahref=”#”onclick=”getTextBoxContent();“>Total Cost Report</a>
</div>
}
If we 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” /> }