Monday, May 18, 2015

ASP.NET MVC View Model / Model not binding on HTTP Post

Problem Scenario:

I am having an issue that when I post to a controller, I loose model binding and everything in my model is NULL. Here is the code that I am using:
View:

@model Models.StateDTO
@using (Html.BeginForm("AddState", "Home",new {area="Admin" }, FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)


State Name



@Html.TextBoxFor(m => m.State)
@Html.ValidationMessageFor(m => m.State)





}

Model:

public class StateDTO
    {        
        public int Id { get; set; }
  
        [Required]
        public string State { get; set; }      
    }

Controller:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddState(StateDTO state)
        {
            if (state != null && ModelState.IsValid)
            {
                //.....
            }
            return View();
        }

Why is everything in my model (i.e. StateDTO state) NULL?

Solution:

One possible reason for view model is NULL :
·         The name of model in action parameter be the same as one of view model's properties

Ex: Here, same name means case insensitive (State is same as state)

Hence, the correct controller action method should be like below:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddState(StateDTO vwState)
        {
            if (vwState != null && ModelState.IsValid)
            {
                //.....
            }
            return View();
        }

Here, "State" property name in model is different then action parameter i.e."vwState"

Hope this helps... Cheers :)


Saturday, May 16, 2015

XBAP Application : How to Configure Visual Studio 2013 to Debug a XAML Browser Application

Scenario:

XBAP Applications does not open in browser when run using Visual studio 2013. Instead, this gives download option..


Solution:


XAML browser applications (XBAPs) run within a partial-trust security sandbox that is restricted to the Internet zone set of permissions.

So, we need to configure Visual Studio to run the XBAP applications in browser..


Steps to configure:



To configure Microsoft Visual Studio to debug an XBAP :
  1. With a project selected in Solution Explorer, on the Project menu, click Properties.
  2. In the Project Designer, click the Debug tab.
  3. In the Start Action section, select Start external program and enter the following:
    C:\WINDOWS\System32\PresentationHost.exe
  4. In the Start Options section, enter the following into the Command line arguments text box:
    -debug filename
    The filename value for the -debug parameter is the .xbap filename; for example:
    -debug c:\example.xbap
Reference: https://msdn.microsoft.com/en-us/library/bb613597(v=vs.110).aspx 

VS Code # Warning - npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead

Error : VS Code - NPM Warning - npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead Solution:  Foll...