I need to add two models to one single view. I am getting the error
An expression tree may not contain a dynamic operation
The coding what I tried mentioned below. There are two models one is a creating model another model is list type model I want to add the list type model in create model. The error I am getting is in Create.cshtml file.
Create.cshtml
@using F17_Support_Desk.Models;
@model dynamic;
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>RequisitionModel</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Username" class="control-label"></label>
<input asp-for="Username" class="form-control" value="@User.Identity.Name" disabled />
<span asp-validation-for="Username" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Department" class="control-label"></label>
<input asp-for="Department" class="form-control" />
<span asp-validation-for="Department" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Details" class="control-label"></label>
<input asp-for="Details" class="form-control" />
<span asp-validation-for="Details" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Period" class="control-label"></label><br />
<input type="radio" name="Period" value="Yes" /> Yes
<input type="radio" name="Period" value="No" /> No
<span asp-validation-for="Period" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EndDate" class="control-label"></label>
<input asp-for="EndDate" class="form-control" />
<span asp-validation-for="EndDate" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-controller="Home" asp-action="Index">Back to List</a><br />
@ViewBag.msg
<h3>@ViewBag.count</h3>
</div>
<table>
@for (int i = 0; i < Model.SectionModels.Count; i++)
{
if (i % 4 == 0)
{
@:<tr></tr>
}
<td>
@Html.CheckBoxFor(x => x.SectionModels[i].IsCheked, new { @class = "custom" })
<label>@Model.SectionModels[i].SectionName</label>
@Html.HiddenFor(x => x.SectionModels[i].SectionId)
@Html.HiddenFor(x => x.SectionModels[i].SectionName)
</td>
}
</table>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Controller
using F17_Support_Desk.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System;
using System.Dynamic;
using System.Linq;
namespace F17_Support_Desk.Controllers
{
public class RequisitionController : Controller
{
private readonly Connection _connection;
public RequisitionController(Connection connection)
{
_connection = connection;
}
public IActionResult Index()
{
dynamic dy = new ExpandoObject();
dy.list = getCheckBox();
dy.req = Create();
return View(dy);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(RequisitionModel requisitionModel, RequisitionSectionModel requisitionSectionModel)
{
try
{
if (!ModelState.IsValid)
{
return View(requisitionModel);
}
Random random = new();
requisitionModel.JobNumber = random.Next(0, 1000000);
requisitionModel.DateOfComplaint = DateTime.UtcNow.Date;
requisitionModel.Username = User.Identity.Name;
_connection.Add(requisitionModel);
_connection.SaveChanges();
int lastId = requisitionModel.JobId;
requisitionSectionModel.JobId_FK = lastId;
_connection.Add(requisitionSectionModel);
_connection.SaveChanges();
if(requisitionModel.JobId == 0)
{
return View(requisitionModel);
}
ViewBag.msg = "Requisition Recorded Your Reference number : " + requisitionModel.JobNumber;
//return RedirectToAction(nameof(Enter name));
}
catch
{
ModelState.AddModelError("", "Something is Wrong");
return View();
}
return View();
}
[HttpGet]
public ActionResult getCheckBox()
{
//var sectionList = _connection.Sections.ToList();
//return View(sectionList);
ListSectionModel model = new ListSectionModel();
model.SectionModels = _connection.Sections.ToList<SectionModel>();
return View(model);
}
[HttpPost]
public ActionResult postCheckBox(ListSectionModel listSection)
{
var list = listSection.SectionModels.Where(x => x.IsCheked == true).ToList<SectionModel>();
return Content(String.Join(",", list.Select(x => x.SectionName)));
}
}
}
Aucun commentaire:
Enregistrer un commentaire