I'm trying to write poll options
to database through common view with poll
. There are two view models
public class PollVM
{
public int PollID { get; set; }
[Display(Name = "Type")]
[Required]
public int Type { get; set; }
[Display(Name = "Status")]
[Required]
public int Status { get; set; }
[Display(Name = "Title")]
[Required]
public string Title { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? DateEdited { get; set; }
public List<PollOptionsVM> PollOptionList { get; set; }
public Poll ToModel(PollVM vm)
{
Poll model = new Poll();
model.id = vm.PollID;//added
model.type = vm.Type;
model.status = vm.Status;
model.title = vm.Title;
model.date_created = DateTime.Now;
model.date_edited = model.date_created;
if (vm.PollOptionList != null)
{
foreach(var item in vm.PollOptionList)
{
var option = new Poll_Options();
option.title = item.Title;
option.date_created = DateTime.Now;
option.date_edited = DateTime.Now;
model.Poll_Options.Add(option);
}
}
return model;
}
}
and
public class PollOptionsVM
{
public int OptionID { get; set; }
public int PollID { get; set; }
[Display(Name = "Title")]
[Required]
public string Title { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateEdited { get; set; }
//public Poll_Options ToModel(PollOptionsVM vm)
//{
// var model = new Poll_Options()
// {
// title = vm.Title,
// date_created = vm.DateCreated
// };
// return model;
//}
public PollOptionsVM ToViewModel(Poll_Options pollOption)
{
PollOptionsVM vm = new PollOptionsVM();
vm.OptionID = pollOption.id;
vm.Title = pollOption.title;
return vm;
}
public List<PollOptionsVM> ToViewModelList(List<Poll_Options> pollOptionList)
{
List<PollOptionsVM> optionList = new List<PollOptionsVM>();
foreach(var item in pollOptionList)
{
optionList.Add(ToViewModel(item));
}
return optionList;
}
}
controller create method
[HttpPost]
public ActionResult Create(PollVM vm)
{
if (ModelState.IsValid)
{
Poll model = new Poll()
{
type = vm.Type,
status = vm.Status,
title = vm.Title,
date_created = vm.DateCreated
};
PollService.Create(vm.ToModel(vm)); //model
}
return View(vm);
}
and view
@model test.ViewModels.PollVM
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
@{
ViewBag.Title = "Create";
}
@section styles{
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
@using (Html.BeginForm("Create", "Poll", FormMethod.Post))
{
<h2>Index</h2>
<div class="container">
<div class="form-group row">
<label for="polltitle" class="col-lg-2 col-md-2 col-sm-12 col-xs-12 col-form-label col-lg-offset-1 col-md-offset-1">
@Html.DisplayNameFor(m => m.Title)
<span class="text-danger">*</span>
</label>
<div class="col-lg-7 col-md-7 col-sm-12 col-xs-12">
@Html.TextBoxFor(m => m.Title, new { @id = "QuestionTextBody", @class = "form-control ckeditor" })
@Html.ValidationMessageFor(m => m.Title, null, new { @class = "text-danger" })
</div>
</div>
<div class="form-group row">
<label for="polltype" class="col-lg-2 col-md-2 col-sm-12 col-xs-12 col-form-label col-lg-offset-1 col-md-offset-1">
@Html.DisplayNameFor(m => m.Type)
<span class="text-danger">*</span>
</label>
<div class="col-lg-7 col-md-7 col-sm-12 col-xs-12">
<div class="custom-control custom-radio">
<label class="custom-control-label" for="polltype1">
@Html.RadioButtonFor(m => m.Type, 1, new { @id = "PollTypeStudent", @class = "custom-control-input", @onclick = "" })
</label>
</div>
<div class="custom-control custom-radio">
<label class="custom-control-label" for="polltype2">
@Html.RadioButtonFor(m => m.Type, 2, new { @id = "PollTypeStaff", @class = "custom-control-input", @onclick = "" })
</label>
</div>
@Html.ValidationMessageFor(m => m.Type, null, new { @class = "text-danger" })
</div>
</div>
<div class="form-group row">
<label for="pollstatus" class="col-lg-2 col-md-2 col-sm-12 col-xs-12 col-form-label col-lg-offset-1 col-md-offset-1">
@Html.DisplayNameFor(m => m.Status)
<span class="text-danger">*</span>
</label>
<div class="col-lg-7 col-md-7 col-sm-12 col-xs-12">
<div class="custom-control custom-radio">
<label class="custom-control-label" for="polltype1">
@Html.RadioButtonFor(m => m.Status, 1, new { @id = "PollStatusActive", @class = "custom-control-input", @onclick = "" })
</label>
</div>
<div class="custom-control custom-radio">
<label class="custom-control-label" for="polltype2">
@Html.RadioButtonFor(m => m.Status, 2, new { @id = "PollStatusInactive", @class = "custom-control-input", @onclick = "" })
</label>
</div>
@Html.ValidationMessageFor(m => m.Type, null, new { @class = "text-danger" })
</div>
</div>
@*<div id="mainpart">
</div>*@
<div id="mainpart">
<div class="container">
<table id="myTable" class="table order-list">
<thead>
<tr>
<td>Option</td>
</tr>
</thead>
<tbody>
@{
if (Model == null || Model.PollOptionList == null || Model.PollOptionList.Count() == 0)
{
<tr>
<td class="col-sm-7">
@Html.TextBox(null, new { @placeholder = "Enter text here", @class = "form-control" })
</td>
</tr>
}
else
{
foreach (var item in Model.PollOptionList)
{
<tr>
<td class="col-sm-7">
@Html.TextBox(item.Title, new { @placeholder = "Enter text here", @class = "form-control" })
</td>
</tr>
}
}
}
</tbody>
<tfoot>
<tr>
<td colspan="5" style="text-align: left;">
<input type="button" class="btn btn-lg btn-block " id="addrow" value="Add Option" />
</td>
</tr>
<tr></tr>
</tfoot>
</table>
</div>
</div>
<button type="submit" value="Submit">Save</button>
<button onclick="history.back()">Back</button>
</div>
}
@section scripts{
<script>
$(document).ready(function () {
var counter = 0;
$("#addrow").on("click", function () {
var newRow = $("<tr>");
var cols = "";
cols += '<td><input type="text" class="form-control" name="name' + counter + '" /></td>';
cols += '<td><input type="button" class="ibtnDel btn btn-md btn-danger " value="Delete"></td>';
newRow.append(cols);
$("table.order-list").append(newRow);
counter++;
});
$("table.order-list").on("click", ".ibtnDel", function (event) {
$(this).closest("tr").remove();
counter -= 1
});
});
$("table.order-list").on("click", ".ibtnDel", function (event) {
$(this).closest("tr").remove();
counter -= 1;
});
function calculateRow(row) {
var price = +row.find('input[name^="price"]').val();
}
function calculateGrandTotal() {
var grandTotal = 0;
$("table.order-list").find('input[name^="price"]').each(function () {
grandTotal += +$(this).val();
});
$("#grandtotal").text(grandTotal.toFixed(2));
}
}
i got problem here
@{
if (Model == null || Model.PollOptionList == null || Model.PollOptionList.Count() == 0)
{
<tr>
<td class="col-sm-7">
@Html.TextBox(null,