Archive for the ‘MVC3’ Category
Monday, December 17th, 2012
VisualStudio 2012 assumes we are using the latest and greatest (version 2) when it comes to the Razer view engine. However when we are opening a project that was created using version 2010 then we were using 1.0 and that is the reason that 2012 is telling us about all the errors it believes we have with our project.
The solution is to edit the web.config file and under “appSettings” tell VisualStudio that this project is using version 1.0
<add key="webpages:Version" value="1.0" /> |
Tags: dotNet, Razor, Razor 1.0, Visual Studio, Visual Studio 2012
Posted in MVC3, Visual Studio 2012 | No Comments »
Wednesday, April 4th, 2012
The RedicrectToAction simply generates a HTTP 302 code witch tells the browser to get the next page.
It is important to remember that when this is used inside a view then we should return RedicrectToAction. This will render the View to the client (and make it redirect) and will stop any further execution (if the RedirectToAction for instance is inside a if statement)
public ActionResult Upload(UploadOrderModel uploadorder)
{
if (uploadorder.IsInvalid)
{
//somthing fishy is going on ...
return RedirectToAction("Index", "Home");
}
//do important things here
return View(orderInfo);
} |
system.web.mvc.controller.redirecttoaction at msdn
Tags: asp.net, MVC3, RedirectToAction, system.web.mvc.controller.redirecttoaction
Posted in MVC3 | No Comments »
Friday, March 2nd, 2012
In order to send mail using a gmail address this can be used:
In Web.config
<system.net>
<mailSettings>
<smtp from="myemail@gmail.com" deliveryMethod="Network">
<network
defaultCredentials="true"
host="smtp.gmail.com"
port="587"
enableSsl="true" />
</smtp>
</mailSettings>
</system.net> |
And then in the code (in this case C# is used)
SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = true; //reads the settings from web.config
smtpClient.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
//and then send the message
smtpClient.Send(mailMessage); |
Or if you don’t wish to save any settings in web.config
string host = "smtp.gmail.com";
int port = 587;
SmtpClient smtpClient = new SmtpClient(host, port);
smtpClient.UseDefaultCredentials = false;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
//and then send the message contained in mailMessage
smtpClient.Send(mailMessage); |
SmtClient at msdn
It might also be interesting to read up on mailmessage
Tags: asp.net, C#, mail, MVC3, System.Net.Mail, System.Net.Mail.MailMessage, System.Net.Mail.SmtpClient
Posted in ASP.NET C#, C#, MVC3 | No Comments »
Monday, February 27th, 2012
When we simply try and do a RedirectToAction and simply passes along a model with this then the model is serialized and sent as GET parameters to the next page. This happens because the RedicrectToAction simply returns a HTTP 302 code witch tells the browser to get the next page. If this is not what you want (perhaps you don’t wish to show the user the parameters or allow user modification), or perhaps you wish to pass along some object or similar.
One way around this is to store the data for the next Action in TempData and re-read it in the next action.
public ActionResult Index(MyModel myModel)
{
TempData["MyModel"] = myModel;
return RedirectToAction("NextStep", "MyController");
}
public ActionResult NextStep(MyModel myModel)
{
myModel = TempData["MyModel"] as MyModel ?? myModel;
return View(myModel);
} |
system.web.mvc.controller.redirecttoaction at msdn
Tags: asp.net, MVC3, RedirectToAction, system.web.mvc.controller.redirecttoaction, system.web.mvc.controllerbase.tempdata, TempData
Posted in ASP.NET C#, MVC3 | No Comments »
Friday, February 24th, 2012
In some cases there is a need to check if the url that is used to access a page corresponds to what we expect.
This can be done like this
if ( Request.RawUrl.ToString() == Url.Action("LogOut", "Account") ){
//The url of the action was used to access this page (and no extra GET parameters or anything hinkey) - and any servername/ip we listen to was used
} |
This differs from getting the URI in the way that this is the RawUrl is only the part after the server (so for http://example.com/url/to/stuffs RawUrl would contain /url/to/stuffs where the URI would contain details about how the user got there.
system.web.httprequest.rawurl at msdn
Tags: asp.net, MVC3, Request, System.Uri, URI, Url.Action
Posted in ASP.NET C#, MVC3 | No Comments »
Saturday, May 7th, 2011
To add htmlattributes to a ActionLink might be needed for instance for the layout of the page.
@Html.ActionLink("Link text", "action", "Controller", null, new {style="float:left"})
//Adding a css class (need the @sign as class is a keyword for the compiler). It is also possible to use a Capitol C in Class but then it fails some validations.
@Html.ActionLink("Link text", "action", "Controller", null, new {@class="myCssClassName"})
//the syntax for the Html.ActionLink (used in this example is)
public static MvcHtmlString ActionLink(
string linkText,
string actionName,
string controllerName,
RouteValueDictionary routeValues,
IDictionary<string, Object> htmlAttributes
) |
Tags: ActionLink, asp.net, C#, Html helper, Html.ActionLink, MVC, MVC3
Posted in ASP.NET C#, MVC3 | No Comments »
Saturday, April 30th, 2011
Using routevalues you can tell the html helper what area you want to create a action link to.
//create a link that will go to AreaName
Html.ActionLink("Link Text", "ActionName", "ControllerName", new { Area = "AreaName" }, new{})
//Default area (= no area)
Html.ActionLink("Link Text", "ActionName", "ControllerName", new { Area = "" }, new{})
//create a link that will go to AreaName and with some htmlAttributes
Html.ActionLink("Link Text", "ActionName", "ControllerName", new { Area = "AreaName" }, new{@class="myCssClass"})
//Definition
public static MvcHtmlString ActionLink(
string linkText,
string actionName,
string controllerName,
RouteValueDictionary routeValues,
IDictionary<string, Object> htmlAttributes
) |
Tags: ActionLink, area, asp.net, C#, Html helper, Html.ActionLink, MVC, MVC3
Posted in ASP.NET C#, MVC3 | No Comments »
Saturday, April 30th, 2011
Using routevales you can change area and/or pass parameters on to the link you wish to hit.
//"normal" Url.Action - creates a link in the area where it is called from (and don't pass any values along)
<a href="@Url.Action("action", "controller")" class="myCssClass">link text</a>
//Url.Action with a that links to another area (area name or empty for the default area); also sets the category to shoes and it passes along the id that this view has.
<a href="@Url.Action("action", "controller", new { Area = "", id = Model.id, category="shoes"})" class="myCssClass">link text</a> |
Tags: area, asp.net, C#, MVC3, routevalues, System.Web.Mvc.UrlHelper, Url helper, Url.Action
Posted in ASP.NET C#, MVC3 | No Comments »
Saturday, April 2nd, 2011
If there is a need to get the URI that the was used to reach the controller then the Request holds that information and it can be retrieved to a URI object
public ActionResult MyAction()
{
var uri = Request.Url;
var port = uri.Port;
var absoluteUri = uri.AbsoluteUri
//... do things here
return View();
} |
Tags: asp.net, C#, MVC, MVC3, Request, System.Uri, URI
Posted in ASP.NET C#, MVC3 | 1 Comment »