i am new in MVC.I try to pass Current Date & Time by using Viewdata.But it does not get my code is here
namespace FirstHelloWorldProjct.Controllers
{
public class HelloWorldController : Controller
{
//
// GET: /HelloWorld/
public ActionResult Index()
{
ViewData["CurrentTime"] = DateTime.Now.ToString();
return View();
}
}
}
And my View Code here
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
This is Hello World...<br/>
The Current Date and time is <%= ViewData["CurrentTime"]%>
</div>
</body>
</html>
And output is here
This is Hello World...
The Current Date and time is <%= ViewData["CurrentTime"]%>
I think In Asp.net MVC we use Razor syntax as metion below.
you need to use #ViewData["CurrentTime"] instead of <%= ViewData["CurrentTime"]%>
Related
I am using Rotativa to generate a PDF of a view with the intention of printing it.
I have the following code
public ActionResult PrintTicket(string tempTickID)
{
//var tick = ctx.vw_printTicket.Where(e => e.noTicket == tempTickID).FirstOrDefault();
// return View(tick);
var report = new ActionAsPdf("PrepareTicket", new { tempTickID = tempTickID });
return report;
}
ActionAsPdf allows me to open the "PrepareTicket" view as a pdf and then I can print.
Problem
The problem for me is this, The pdf takes over my entire page and while I can print I don't have access to my program's menus anymore cause it's now a PDF view.
Questions
Is it possible that I call the print dialog automatically instead of showing the pdf?
I think will work for my situation.
Regards
Hi I have tried to create a sample which will solve your issue.
Model
public class Ticketinfo
{
public string name { get; set; }
public int quantity { get; set; }
}
Created a Controller which has 3 Action Method
public class GenerateController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult PrintTicket(string tempTickID)
{
return new ActionAsPdf("RotativaPartialViewAsPdf", new { tempTickID = tempTickID });
}
public ActionResult RotativaPartialViewAsPdf(string tempTickID)
{
Ticketinfo Ticketinfo = new Ticketinfo()
{
name = "Demo",
quantity = 5
};
return PartialView("_RotativaPartialViewAsPdfl", Ticketinfo);
}
}
Partial View
#model WebApplication6.Models.Ticketinfo
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<table class="table">
<tr class="info">
<td>Lot</td>
<td>Name</td>
<td>Quantity</td>
</tr>
<tr class="success">
<td>#Model.name</td>
<td>#Model.quantity</td>
</tr>
</table>
</div>
</body>
</html>
Index View
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<iframe src="#Url.Action("PrintTicket", "Generate", new {tempTickID = "1"})"
width="800px" height="600px">
</iframe>
</div>
</body>
</html>
Output
I have a problem. In order to make it understandable, I want to denote my layout. layout is as follows:
Menu side is implemented by NavController. Here is the content of the NavController:
public class NavController : Controller
{
private IProductRepository repository;
public NavController(IProductRepository repo) {
repository = repo;
}
[ChildActionOnly]
public PartialViewResult Menu(string category = null)
{
ViewBag.SelectedCategory = category;
//IEnumerable<string> categories = repository.Products
//.Select(x => x.ProductCategory)
//.Distinct()
//.OrderBy(x => x);
IEnumerable<string> categories = Actions.GetirTumAksiyonları();
return PartialView(categories);
}
}
At the Menu action method, some strings populated and become visible at the Menu side.
There are many controllers defined in my project that manipulates the Content of the project, and they have buttons that post back to server. Whenever I click at one of these buttons, menu strings permenantly populated. Here is my _Layout.cs:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>#ViewBag.Title</title>
<link href="~/Content/Site.css" type="text/css" rel="stylesheet" />
<link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="~/Scripts/jquery-ui-1.8.24.min.js"></script>
</head>
<body>
<div id="header">
<div class="title">Lojman Bilgi Sistemi</div>
</div>
<div id="categories">
#{ Html.RenderAction("Menu", "Nav"); }
</div>
<div id="content">
#RenderBody()
</div>
How can I overcome the issue? thanks in advance.
You can write your Menu method this way. Cheers :)
[ChildActionOnly]
public PartialViewResult Menu(string category = null)
{
// Logic
}
I am getting an error:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MyProject.Models.MyRecord]', but this dictionary requires a model item of type 'MyProject.Models.MyRecord'
When running my MVC app. Here is my view:
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MyProject.Models.MyRecord>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>Details</title>
</head>
<body>
<div>
<% foreach (MyProject.Models.MyRecord r in Model){%>
<%=r.Property1 %>
<%} %>
</div>
</body>
</html>
My controller action is as follows:
public ActionResult DisplayRecords(string id)
{
MyRecordContext rc = new MyRecordContext();
List<MyRecord> rl = rc.MyRecords.Where(x => x.RecordID == id).ToList();
return View(rl);
}
MyRecord class is decalred as: public class MyRecord : IEnumerable
Can anyone please point me in the right direction?
how return razor code for layout of view instead of master page name
for example instead of
return View("Index", "_Layout");
use this code
return View("Index", #"
#using System.Diagnostics
#using iaumahallat.Helper
<!DOCTYPE html>
<html lang="en">
<head>
<title>#ViewBag.SiteTitle - #ViewBag.Title</title>
<meta name="description" content="#ViewBag.MetaDescription">
#Scripts.Render("~/bundles/modernizr")
</head>
<body>
#RenderBody()
#RenderSection("scripts", required: false)
</body>
</html>
");
One way is to use RazorEngine and parse the razor source code, then get the corresponding Html String to display it on the view or layout-
Razor.SetTemplateBase(typeof(HtmlTemplateBase<>));
string template =
#"<html>
<head>
<title>Hello #Model.Name</title>
</head>
<body>
Email: #Html.TextBoxFor(m => m.Email)
</body>
</html>";
var model = new PageModel { Name = "World", Email = "someone#somewhere.com" };
string result = Razor.Parse(template, model);
Then use result in the page of your interest.
I have a simple .cshtml page and My problem is when I run this page alert is not shown. Here is the .cshtml page code:
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>ChartDataJSON</title>
<script type="text/javascript">
alert('hello');
</script>
</head>
<body>
<div>
<div id="chart1" ></div>
</div>
</body>
</html>
Also I have a controller class through which this view is generated. The lines written in that class are:
public class jqPlotController : Controller
{
//
// GET: /jqPlot/
public ActionResult Index()
{
return View();
}
public ActionResult ChartDataJSON()
{
var chartData = new List<jqplotModel>();
var point1 = new jqplotModel { Date = DateTime.Now.Date.ToString("yyyy-MM-dd h:mmtt"), Demand = Convert.ToDouble(1), Supply = Convert.ToDouble(3) };
var point2 = new jqplotModel { Date = DateTime.Now.AddDays(10).Date.ToString("yyyy-MM-dd h:mmtt"), Demand = Convert.ToDouble(2), Supply = Convert.ToDouble(4) };
var point3 = new jqplotModel { Date = DateTime.Now.AddDays(31).Date.ToString("yyyy-MM-dd h:mmtt"), Demand = Convert.ToDouble(6), Supply = Convert.ToDouble(6) };
var point4 = new jqplotModel { Date = DateTime.Now.AddDays(106).Date.ToString("yyyy-MM-dd h:mmtt"), Demand = Convert.ToDouble(4), Supply = Convert.ToDouble(2) };
chartData.Add(point1);
chartData.Add(point2);
chartData.Add(point3);
chartData.Add(point4);
return Json(chartData, JsonRequestBehavior.AllowGet);
}
}
Is this due to the “return” keyword in the controller class which stops alert pop up from appearing on the web page.
Has anybody faced this type of issue before. Can somebody help me in figuring out the problem?
Edit:
The view is added with respect to ChartDataJSON method of controller class.
Update: I have made following changes in view and the controller class is as mentioned above.
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>ChartDataJSON</title>
<script type="text/javascript">
$.ajax("/jqPlot/ChartDataJSON")
.done(function (data) {
// do something with the data.
alert("success");
})
</script>
</head>
<body>
<div>
<div id="chart1" ></div>
</div>
</body>
</html>
When returning JSON result you will not render out any Views (HTML markup). To return the HTML markup you must write this
return View("name of cshtml file without .cshtml");
where [name of view] is the name of the cshtml file you were referring to (without the .cshtml extension), and the model can be the data you want to use in the cshtml file.
To retrieve the data using ajax you can use this command as a example
$.ajax( "/jqPlot/ChartDataJSON" )
.done(function(data) {
// do something with the data.
alert( "success" );
})
Update:
To load the view you have to call an action that returns and renders the view. Lets say your view is named Index.cshtml and its full path is "Views/jqPlotController/Index.cshtml
Then all you have to do is call the URL of the action, http://[nameofdomain]/Index .
Then the view should be loaded and the javascript function trigger and call the action ChartDataJSON, which will return the JSON result and in this case the alert is triggered if the call to the action is successfull.