Click here to Skip to main content
15,885,757 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: Web API: Can we generate token without using identity Pin
Nathan Minier5-Mar-18 2:00
professionalNathan Minier5-Mar-18 2:00 
GeneralRe: Web API: Can we generate token without using identity Pin
Mou_kol5-Mar-18 21:13
Mou_kol5-Mar-18 21:13 
General.NET Core fits in the .NET Ecosystem Pin
Priyanka Kale26-Feb-18 23:15
Priyanka Kale26-Feb-18 23:15 
QuestionAshraf Pin
Member 1206559124-Feb-18 23:53
Member 1206559124-Feb-18 23:53 
AnswerRe: Ashraf Pin
Richard MacCutchan25-Feb-18 3:47
mveRichard MacCutchan25-Feb-18 3:47 
AnswerRe: Ashraf Pin
ZurdoDev26-Feb-18 2:04
professionalZurdoDev26-Feb-18 2:04 
QuestionRe: Ashraf Pin
ZurdoDev26-Feb-18 2:05
professionalZurdoDev26-Feb-18 2:05 
QuestionASP.Net MVC: How to show checkboxes selection in webgrid column after postback data Pin
Mou_kol19-Feb-18 7:17
Mou_kol19-Feb-18 7:17 
I have developed a tabular UI with webgrid. i am showing student information through webgrid. i am showing multiple checkboxes for hobbies in each row of webgrid. when i select hobbies and click submit button then i saw hobbies selection is not going to action.

i guess there is my mistake in view model class design. please have a look at my code and tell me which area i need to change in code.

i want all hobbies should go to action when i click submit button and selected hobbies also should post to action for each student. a student may have multiple hobbies selected.

here is my viewcode
C#
@model MVCCRUDPageList.Models.StudentListViewModel
@{
    ViewBag.Title = "Index";
}

<h2>Student View Model</h2>

@using (Html.BeginForm("Index", "WebGridMoreControls", FormMethod.Post))
{
    var grid = new WebGrid(Model.Students, canSort: false, canPage: false);
    var rowNum = 0;
    var SelectedHobbies = 0;

    <div id="gridContent" style=" padding:20px; ">
        @grid.GetHtml(
        tableStyle: "table",
        alternatingRowStyle: "alternate",
        selectedRowStyle: "selected",
        headerStyle: "header",
        columns: grid.Columns
        (
            grid.Column(null, header: "Row No", format: item => rowNum = rowNum + 1),
            grid.Column("ID", format: (item) => @Html.TextBoxFor(m => m.Students[rowNum - 1].ID, new { @class = "edit-mode" })),
            grid.Column("Name", format: (item) => @Html.TextBoxFor(m => m.Students[rowNum - 1].Name, new { @class = "edit-mode" })),

             grid.Column("Country", format: (item) =>
                  @Html.DropDownListFor(x => x.Students[rowNum - 1].CountryID,
                  new SelectList(Model.Country, "ID", "Name", item.CountryID),
                 "-- Select Countries--", new { id = "cboCountry", @class = "edit-mode" })),

            grid.Column(header: "Hobbies",
            format: @<text>

            Hobbies
            @foreach (var hobby in Model.Hobbies)
            {
                <div class="checkbox">
                    <label>
                        @Html.HiddenFor(e => e.Hobbies)
                        <input type="checkbox"
                               name="Hobbies"
                               value="@hobby.ID" /> @hobby.Name
                    </label>
                 </div>
            }
            </text>)
        ))

        <input type="submit" value="Submit" />
    </div>

}



Action code
public class WebGridMoreControlsController : Controller
{
// GET: WebGridMoreControls
public ActionResult Index()
{
StudentListViewModel osvm = new StudentListViewModel();
return View(osvm);
}

[HttpPost]
public ActionResult Index(StudentListViewModel oStudentListViewModel)
{
return View(oStudentListViewModel);
}
}
View model code
C#
public class StudentListViewModel
{
    public IList<Student> Students { get; set; }
    public List<Country> Country { get; set; }

    public IList<Hobby> SelectedHobbies { get; set; }
    public IList<Hobby> Hobbies { get; set; }

    public StudentListViewModel()
    {
        Students = new List<Student>
        {
            new Student{ID=1,Name="Keith",CountryID=0,Hobby=0},
            new Student{ID=2,Name="Paul",CountryID=2,Hobby=0},
            new Student{ID=3,Name="Sam",CountryID=3,Hobby=0}
        };

        Country = new List<Country>
        {
            new Country{ID=1,Name="India"},
            new Country{ID=2,Name="UK"},
            new Country{ID=3,Name="USA"}
        };

        Hobbies = new List<Hobby>
        {
            new Hobby{ID=1,Name="Football"},
            new Hobby{ID=2,Name="Hocky"},
            new Hobby{ID=3,Name="Cricket"}
        };

    }
}

Model code
C#
<pre lang="c#">
public class Student
{
public int ID { get; set; }
[Required(ErrorMessage = "First Name Required")]
public string Name { get; set; }

public int CountryID { get; set; }
public int Hobby { get; set; }
}

public class Country
{
public int ID { get; set; }
public string Name { get; set; }
}

public class Hobby
{
public int ID { get; set; }
public string Name { get; set; }
}


please help me to rectify view, viewmodel and model class code. thanks
Questionread json file using newtonsoft.json Pin
alpacaheng18-Feb-18 21:49
alpacaheng18-Feb-18 21:49 
SuggestionRe: read json file using newtonsoft.json Pin
Richard MacCutchan18-Feb-18 22:26
mveRichard MacCutchan18-Feb-18 22:26 
AnswerRe: read json file using newtonsoft.json Pin
F-ES Sitecore19-Feb-18 0:08
professionalF-ES Sitecore19-Feb-18 0:08 
QuestionHow to update local json file? Pin
alpacaheng18-Feb-18 21:16
alpacaheng18-Feb-18 21:16 
AnswerRe: How to update local json file? Pin
F-ES Sitecore19-Feb-18 0:10
professionalF-ES Sitecore19-Feb-18 0:10 
QuestionHow to prevent "OnSelectedIndexChanged" event triggered in DropDownCheckBoxes. When DropDownCheckBoxes Bound? Pin
Member 1368202316-Feb-18 9:37
Member 1368202316-Feb-18 9:37 
QuestionSearchable dropdowncheckboxes (like AutoFilter) in asp.net Pin
Member 1368202316-Feb-18 9:31
Member 1368202316-Feb-18 9:31 
QuestionFiltering the data in GridView using Linq query without DataBase in VB.NET Pin
Member 1368202316-Feb-18 9:27
Member 1368202316-Feb-18 9:27 
QuestionRe: Filtering the data in GridView using Linq query without DataBase in VB.NET Pin
Maciej Los17-Feb-18 2:51
mveMaciej Los17-Feb-18 2:51 
QuestionHow browser cache web site pages Pin
Mou_kol16-Feb-18 1:40
Mou_kol16-Feb-18 1:40 
Questionwhat is advantage of loading js file from CDN Pin
Mou_kol16-Feb-18 1:38
Mou_kol16-Feb-18 1:38 
QuestionPls Provide me business logic method for reset password or forget password by user id... Pin
Member 1367430012-Feb-18 4:01
Member 1367430012-Feb-18 4:01 
AnswerRe: Pls Provide me business logic method for reset password or forget password by user id... Pin
OriginalGriff12-Feb-18 4:03
mveOriginalGriff12-Feb-18 4:03 
GeneralRe: Pls Provide me business logic method for reset password or forget password by user id... Pin
Member 1367430012-Feb-18 4:28
Member 1367430012-Feb-18 4:28 
AnswerRe: Pls Provide me business logic method for reset password or forget password by user id... Pin
Mukthahar Shaik12-Feb-18 7:47
Mukthahar Shaik12-Feb-18 7:47 
GeneralRe: Pls Provide me business logic method for reset password or forget password by user id... Pin
Member 1367430012-Feb-18 7:58
Member 1367430012-Feb-18 7:58 
GeneralRe: Pls Provide me business logic method for reset password or forget password by user id... Pin
Mukthahar Shaik12-Feb-18 8:11
Mukthahar Shaik12-Feb-18 8:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.