MVC3 Valums Ajax File Upload

I figured it out. this works in IE and Mozilla. [HttpPost] public ActionResult FileUpload(string qqfile) { var path = @”C:\\Temp\\100\\”; var file = string.Empty; try { var stream = Request.InputStream; if (String.IsNullOrEmpty(Request[“qqfile”])) { // IE HttpPostedFileBase postedFile = Request.Files[0]; stream = postedFile.InputStream; file = Path.Combine(path, System.IO.Path.GetFileName(Request.Files[0].FileName)); } else { //Webkit, Mozilla file = Path.Combine(path, qqfile); … Read more

How can I create a route constraint of type System.Guid?

Create a RouteConstraint like the following: public class GuidConstraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { if (values.ContainsKey(parameterName)) { string stringValue = values[parameterName] as string; if (!string.IsNullOrEmpty(stringValue)) { Guid guidValue; return Guid.TryParse(stringValue, out guidValue) && (guidValue != Guid.Empty); } } return false; }} Next when adding … Read more

Correct way to use HttpContext.Current.User with async await

As long as your web.config settings are correct, async/await works perfectly well with HttpContext.Current. I recommend setting httpRuntime targetFramework to 4.5 to remove all “quirks mode” behavior. Once that is done, plain async/await will work perfectly well. You’ll only run into problems if you’re doing work on another thread or if your await code is … Read more

How can I use Html.Action?

You should look at the documentation for the Action method; it’s explained well. For your case, this should work: @Html.Action(“GetOptions”, new { pk=”00″, rk=”00″ }); The controllerName parameter will default to the controller from which Html.Action is being invoked. So if you’re trying to invoke an action from another controller, you’ll have to specify the … Read more

How to use a ViewBag to create a dropdownlist?

You cannot used the Helper @Html.DropdownListFor, because the first parameter was not correct, change your helper to: @Html.DropDownList(“accountid”, new SelectList(ViewBag.Accounts, “AccountID”, “AccountName”)) @Html.DropDownListFor receive in the first parameters a lambda expression in all overloads and is used to create strongly typed dropdowns. Here’s the documentation If your View it’s strongly typed to some Model you … Read more

How to pass parameters to a custom ActionFilter in ASP.NET MVC 2?

This is a way to make this work. You have access to the ControllerContext and therefore Controller from the ActionFilter object. All you need to do is cast your controller to the type and you can access any public members. Given this controller: public GenesisController : Controller { [CheckLoggedIn()] public ActionResult Home(MemberData md) { return … Read more

Unable to find the requested .Net Framework Data Provider. It may not be installed. – when following mvc3 asp.net tutorial

I was able to solve a problem similar to this in Visual Studio 2010 by using NuGet. Go to Tools > Library Package Manager > Manage NuGet Packages For Solution… In the dialog, search for “EntityFramework.SqlServerCompact”. You’ll find a package with the description “Allows SQL Server Compact 4.0 to be used with Entity Framework.” Install … Read more

How to post ASP.NET MVC Ajax form using JavaScript rather than submit button

I’m going to assume that your lack of quotes around the selector is just a transcription error, but you should check it anyway. Also, I don’t see where you are actually giving the form an id. Usually you do this with the htmlAttributes parameter. I don’t see you using the signature that has it. Again, … Read more

Html.Raw() in ASP.NET MVC Razor view

Html.Raw() returns IHtmlString, not the ordinary string. So, you cannot write them in opposite sides of : operator. Remove that .ToString() calling @{int count = 0;} @foreach (var item in Model.Resources) { @(count <= 3 ? Html.Raw(“<div class=\”resource-row\”>”): Html.Raw(“”)) // some code @(count <= 3 ? Html.Raw(“</div>”) : Html.Raw(“”)) @(count++) } By the way, returning … Read more

ASP.net MVC – Display Template for a collection

Like this: @Html.DisplayFor(m => m.Children, “YourTemplateName”) or like this: [UIHint(“YourTemplateName”)] public IEnumerable<ChildModel> Children { get; set; } where obviously you would have ~/Views/Shared/DisplayTemplates/YourTemplateName.cshtml: @model IEnumerable<ChildModel> <table> <tr> <th>Property 1</th> <th>Property 2</th> </tr> … </table>