Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I will display a view, when I click on checkbox

I tried like this, but not work,
I need your help to fix the problem

Models:

C#
public class DisplayData
   {
       public bool ID { get; set; }
       public DisplayData(bool ID)
       {
          this.ID = ID;
       }
   }

public class Element
{        
    public string Descripcion { get; set; }
}


HomeController:

C#
public ActionResult Index()
    {
       DisplayData Display = new DisplayData(false);
       return View(Display);
    }


Index.cshtml:

HTML
@model AppTwitter.Models.DisplayData

  <script src="@Url.Content("~/Scripts/myCheckbox.js")" type="text/javascript"></script>

 @Html.CheckBoxFor(
 x => x.ID, 
 new {
    data_url = Url.Action("PartialDemo", "PartialDemo"), 
    id = "mycheckbox" 
  }


myCheckbox.js:

JavaScript
$(function () {
$('#mycheckbox').change(function () {
    var data = {};
    data[$(this).attr('name')] = $(this).is(':checked');

    $.ajax({
        url: $(this).data('url'),
        type: 'POST',
        data: data,
        success: function (result) {

        }
    });
});
});


PartialDemoController.cs

C#
public ActionResult PartialDemo()
    {                            
            var element = new Element();
            element.Descripcion = "Descripcion";
            return View(element);
    }


PartialDemo.cshtml:

HTML
@model AppTwitter.Models.Element

  <div class="editor-label">
       @Html.LabelFor(model => model.Descripcion )
   </div>

   <div class="editor-field">
       @Html.EditorFor(model => model.Descripcion )

       @Html.ValidationMessageFor(model => model.Descripcion )
   </div>



Thanks,
Posted
Comments
saisne 21-Jun-12 12:49pm    
@Tim Corey any help, to fixe this probleme!

1 solution

Try this

$('#mycheckbox').click(function () {
$.ajax({
url: $(this).data('url'),
type: 'POST',
data: 'Name=' + $(this).attr('name') + '&Id=' + $(this).is(':checked'),
success: function (result) {
}
});
});

[HttpPost]
C#
public ActionResult PartialDemo(string Name, bool Id)
    {
            var element = new Element();
            element.Descripcion = "Descripcion";
            return View(element);
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900