everyone. I'm trying to build simple app that shows relations between apps and access objects. And and I have some some problems with post data.
The main idea is to show edit window with app name and multiple categorized check boxes. User will check some of them, and this info will be saved in db.
The promblem is how to get data from these checkboxes and put it into my model?
I got these models
public class AccessDataType
{
public int Id { get; set; }
public string Name { get; set; }
public virtual IList<AccessDataParameter> AccessDataParameters { get; set; }
}
public class AccessDataParameter
{
public int Id { get; set; }
public string Name { get; set; }
public virtual AccessDataType AccessDataType { get; set; }
public virtual IList<Application> Applications { get; set; }
}
public class Application
{
public int Id { get; set; }
public int InternalId { get; set; }
public string Name { get; set; }
public virtual IList<AccessDataParameter> AccessDataParameters { get; set; }
}
I have AccessDataType that includes AccessDataParameter entries and Application class that includes some of AccessDataParameter entries.
I decided to create ApplicationAndApplicationLinkViewModel class
public class ApplicationAndApplicationLinkViewModel
{
public IList<ApplicationLink> ApplicationLinks { get; set; }
public Application Application { get; set; }
}
in my Application controller i use this method to edit my view model
public ActionResult Edit(int id)
{
ApplicationAndAccessDataTypesViewModel model = new ApplicationAndAccessDataTypesViewModel
{
Application = dbContext.Applications.Find(id),
AccessDataTypes = dbContext.AccessDataTypes.Include(a => a.AccessDataParameters).ToList()
};
return View(model);
}
And there I have the first problem. I don't know how to create checkboxes that matches my condition. I use double for loop to print AccessDataTypes and related AccessDataParameters. For each AccessDataParameter a check box needs to be created. As I undestand, boolean value needs to be passed into CheckBoxFor/CheckBox. But my models don't have them.
@for (int i = 0; i < Model.AccessDataTypes.Count; i++)
{
<div class="form-group">
@Html.DisplayFor(model => model.AccessDataTypes[i].Name)
<div class="col-md-offset-2 col-md-10">
@for (int j = 0; j < Model.AccessDataTypes[i].AccessDataParameters.Count; j++)
{
@Html.HiddenFor(m => Model.AccessDataTypes[i].AccessDataParameters[j].Id);
@Html.HiddenFor(m => Model.AccessDataTypes[i].AccessDataParameters[j].Name);
@Html.CheckBoxFor( -- boolean here --)
}
</div>
</div>
}
And I don't need that boolean at all. Because checkboxes must be filled using data in AccessDataParameter class. I mean checkbox AccessDataTypes[i].AccessDataParameters[j] must be checked if in AccessDataParameter in Applications property id (that comes in controller) is found.
Aucun commentaire:
Enregistrer un commentaire