MVC3

...now browsing by category

 

Observations: @Html.EditorFor and @Html.TextBoxFor in MVC4

Monday, November 24th, 2014

A few tricks learned today when I was working on a email template manager project:

1) If an Action method was marked as [HttpPost] to handle the post event from a form, then there must be a same name Action without [HttpPost] attribute existing in the same controller or you will get a “Page cannot be displayed” error and there was very little debug info to go about.

For example, I have

[ValidateInput(false)]
[HttpPost]
public ActionResult FileUpload(EmailTemplateModel model)
{ //doing files upload }
then I must have a plain action with same name but different signature:

public ActionResult FileUpload()
{
EmailTemplateModel model = new EmailTemplateModel();

model.Attachments= PrepareAttachmentNewModel();

return View(model);

}

or I will get “Page cannot be displayed” error when I try to submit the form with one or multiple “file” html element.

2) Interestingly, when I return the model to view, only when I used @Html.EditorFor, I will be able to render the properties related to the file upload into each textbox; but then the other textboxes will become empty for those fields that were are already there on the form prior to posting the form. Instead, using @Html.TextBoxFor() will retain those values.

Have yet to understand the mechanism underneath, but for now these are my observations and just a quick note first.

Smtp Permission error at Winhost

Friday, December 7th, 2012

When I was developing my ASP.Net app on local machine,  I had no problem sending email by calling directly the SMTP relay server assigned by my hosting company Winhost, with port set to 587 of course. But when I deployed the app to the winhost server, the email tool threw this exception :

Request for the permission of type ‘System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′ failed.

I did suspect some some security permission to Smtp related assembly and have done some search toward that direction. First I got a lead from some post via Google search and one suggested added this entry into the <system.web> section; but that did not work, so I am not going to give reference to this post.

<securityPolicy>
<trustLevel name=”Full” policyFile=”internal”/>
</securityPolicy>

Today, I went into Windost support site and did another search on their forum, and they pointed me to using this instead:

<system.web>

<trust level=”Full” />

</system.web>

Yes, that was all it needed!

Setting up Elmah SQL Server objects manually

Tuesday, December 4th, 2012

1. Run Elmah.axd on localhost and got this error,”Could not find stored procedure ‘ELMAH_GetErrorsXml'”.
What happened was that the Elmah sql objects were not installed properly when I run NuGet package-install elmah from inside VS2010 IDE.

2. Remedy was downloading the elmah-1.2-db-sqlserever.sql script from here
4. Open SQL Server 2008 Management studio and connected to the remote database hosted at Winhost
5. Opened the Elmah db sql script file and executed it on the remote database.
6. Now returned to localhost/Elmah.axd and error log is showing.

MvcCheckBoxList for MVC3

Thursday, May 31st, 2012

As of today, there is not existing Html exentension for CheckBoxList. You can find @Html.CheckBoxFor but you don’t see @Html.CheckBoxListFor<>, untill, you download and install third party dll or build your own. Today I found one that you can get from NuGet, by reading up article at CodeProject http://www.codeproject.com/Articles/292050/MVC3-Html-CheckBoxList-custom-extension

MVC3 @Html.CheckBoxList extension isntalled from NuGet:

PM> install-package MvcCheckBoxList
Successfully installed ‘MvcCheckBoxList 1.4.2.3′.
Successfully added ‘MvcCheckBoxList 1.4.2.3′ to ETEF.Web.

 

Once MvcCheckBoxList was intalled, it shows up in the project reference as “MvcCheckBoxList”, no particular namespace.
How to use it?

On view:
<div>
<div>
<h3>Roles</h3>
@{
var roles = Model.AvailableRoles.Select(r=>new SelectListItem{Value=r.RoleName,Text=r.RoleName}).ToList();

}

@Html.CheckBoxList(“roles”,roles)
</div>
</div>

on controller:

how to intercept the checkboxes that are selected?
After user clicks on Submit button, the checked boxes were posted as an array of strings. Example in my ETEF.Web WeblinkController code:

[HttpPost]
public ActionResult EditLink(WeblinkModel model, string[] roles)
{

using (ETEF.Data.Repositories.AccessControlRepository acr = new Data.Repositories.AccessControlRepository(GlobalSettings.Database.EntitiesConnectionString))
{
Weblink lnk = new Weblink();
lnk.LinkID = model.WebLinkID;
lnk.LinkText = model.LinkCaption;
lnk.LinkTypeID = model.LinkTypeID;
lnk.NavigateUrl = model.NavigateUrl;
lnk.Roles = GlobalFunctions.ConverArrayToString(roles);
lnk.StatusID = 1;
lnk.DisplayOrder = model.DisplayOrder;
lnk.Description = model.Description;
//lnk.ImageUrl = model.ImageUrl;
int ret = acr.SaveWeblink(lnk);
if (ret > 0)
model.Message = DisplayMessage(“Success”, “Form data has been saved successfully.”, MessageBoxTypes.Success);
else
model.Message = DisplayMessage(“Problem”, “Form data was not saved successfully. Please check your input and try it again.”, MessageBoxTypes.Error);

if (model.WeblinkTypes == null)
{
model.WeblinkTypes = acr.GetWeblinkTypes();
model.AvailableRoles = acr.GetAvailableRoles();

}

}
return View(model);

}