Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Thursday, March 3, 2016

@Html.CheckBoxFor( ) helper method problem when posting to Controller method with Bind(Include=...) annotation

@Html.CheckBoxFor(model => model.IsDeposit) will generate html
<input type="checkbox" name="IsDeposit" value="true" />
<input type="hidden"   name="IsDeposit" value="false" />
given that IsDeposit is a boolean value. Then the form is submitted to a method in controller such as
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include ="PaymentID,IsDeposit")] Payment payment)
{ . . . }        
The problem is that the checkbox value will always be false whether the checkbox is checked or not.

The cause of this problem seems to be the IsDeposit in the Bind(Include=...) annotation is not handled properly yet in ASP.NET MVC. @Html.CheckBoxFor(..) creates two elements with the same name attribute (checkbox and hidden). Possibly only the hidden element is accepted to the method when Bind(Include=..) is used. I tried including "IsDeposit" twice (for example, Bind[Include=IsDeposit,IsDeposit,,]) just in case, but it did not help.

The best bet is to avoid using the Bind(Include=...) annotation when @Html.CheckBoxFor() helper method is used. Alternatively, you can also create the checkbox element manually yourself instead of using the helper method @Html.CheckBoxFor(). For example,
<input type="checkbox" name="IsDeposit" id="IsDeposit" value="true" />

Friday, October 16, 2015

Genenrate full Url (absolute Url) using C# in ASP.NET MVC

A quick way to generate full Url (absolute Url) in ASP.NET MVC:

Url.Action("Details", "Product", new { id = p.ProductID}, Request.Url.Scheme)


If without id, simply use null in the parameter.

Url.Action("Details", "Product", null, Request.Url.Scheme)

Monday, July 7, 2014

MVC Ad-hoc DropDownList for PageSize

Example of Razor DropDown List for PageSize in an Index View using ViewBag.

Here is a controller
public class ProductController : Controller
{
  public ActionResult Index( int? page, int? pagesize )
  {
    ...
    int pageSize = pagesize ?? 5;
 
    List<SelectListItem> items = new List<SelectListItem>{
      new SelectListItem{ Text="5", Value="5" },
      new SelectListItem{ Text="10", Value="10" }
    }
    
    // items = List<SelectListItem>
    // "Value" = ValueText
    // "Text" = DisplayText
    // pagesize = SelectedListItem's Value from querystring
    ViewBag.PageSizeList = new SelectList( items, "Value", "Text", pagesize );
    
    ViewBag.CurrentPageSize = pageSize
  }
}
Here is the view
@using PagedList
@using PagedList.Mvc
@model IEnumerable

<form id="form1" action=@Url.Action("Index", "Product") method="get">
...
  @Html.DropDownList( "pagesize", (SelectList)ViewBag.PageSizeList )
  // We keep DropDown list's name separate from ViewBag element's name


  @foreach ( var item in Model ) {  }
  <table><tbody>
  <tr><td>...</td></tr>
  </tbody></table>

  @Html.PagedListPager( (IPagedList)Model, 
    page => Url.Action( "Index", new { page, pagesize = @ViewBag.CurrentPageSize })

</form>
<script type="text/javascript" src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js">
</script>
<script type="text/javascript">
  $(function(){
    // When onchange event occurs on <select id="pagesize">, 
    // simply submit the form, which will submit the new pagesize value as querystring.
    $('#pagesize').on('change', function(){
      $('#form1').submit();
    });
  });
</script>

Monday, November 11, 2013

Login prompt keeps coming when developing Intranet MVC4 Website on IIS Express

I created a MVC 4 project using Visual Studio 2012 on Windows 7 Computer that is a standalone and not a member of an Active Directory domain. I selected the intranet application template for the MVC4 application in Visual Studio 2012, successfully created initial aplication with no error and adjusted project property settings as the initial ReadMe.txt file showed (i.e., Anonymous Authentication = Disabled, Windows Authentication = Enabled).

However, when running the website using IIS Express, the Login Prompt keeps coming up. I thought this nuisance was due to the fact that my computer was not a member of any domain and therefore, the browser is not treating my request as intranet traffic. When I enter my local Windows user name and password, the IIS Express lets me in and I see my MVC4 website loaded in IE properly. Still, having to enter my credential each time I press Ctrl + F5 is a nuisance to take care of for my productivity.


Workaround: 
  1. Run IE. Go to [Tools] --> [Internet Options] menu. Select [Security] tab.
  2. Choose [Trusted Sites] and click the [Sites] button.
  3. Add "http://localhost" to your trusted website. Click [close] button.
  4. When back to the [Security] tab, make sure the [Trusted sites] is still selected. 
  5. Click [Custom level...] button while under the [Trusted sites] context. 
  6. Scroll down to "User Authentication". Select "Automatic logon with current user name and password"

Through another test, I confirmed that when you develop a intranet website using VS 2012 and IIS Express on a computer that is a member of an Active Directory domain, you do not run into this issue of unnecessary login prompt. This issue only happens when you develop on a stand-alone Windows machines.