提问人:user22647583 提问时间:9/28/2023 更新时间:9/28/2023 访问量:18
传递到字典中的模型项的类型为“VillaSite.Models.ReservationForm”,但此字典需要类型为“System”的模型项
The model item passed into the dictionary is of type 'VillaSite.Models.ReservationForm', but this dictionary requires a model item of type 'System
问:
当我从网页提出预订请求时,它给出了以下错误。我在电脑上写了网页代码。我调试。没有问题,该站点在本地ISP上完美运行。但是当我将站点代码上传到托管公司时,站点的某些部分会出现错误。我再次检查计算机上的代码以查看是否有错误,然后再次生成和调试。没有错误。因此,在托管公司上出现错误的代码在我的本地计算机和 ISP 上正常工作。我不知道该怎么办。这是我从托管公司获得的错误代码。
“/”应用程序中的服务器错误。 传递到字典中的模型项的类型为“VillaSite.Models.ReservationForm”,但此字典需要类型为“System.Collections.Generic.IEnumerable'1[VillaSite.Models.ReservationForm]”的模型项。 说明:执行当前 Web 请求期间发生未经处理的异常。请查看堆栈跟踪,了解有关错误及其在代码中的来源的详细信息。
异常详细信息:System.InvalidOperationException:传递到字典中的模型项的类型为“VillaSite.Models.ReservationForm”,但此字典需要类型为“System.Collections.Generic.IEnumerable'1[VillaSite.Models.ReservationForm]”的模型项。
源错误:
在执行当前 Web 请求期间生成了未经处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常来源和位置的信息。
堆栈跟踪:
[InvalidOperationException:传递到字典中的模型项的类型为“VillaSite.Models.ReservationForm”,但此字典需要类型为“System.Collections.Generic.IEnumerable1.SetModel(Object value) +175”的模型项
System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary字典) +107
System.Web.Mvc.WebViewPage1 筛选器、 Int32 filterIndex、 ResultExecutingContext preContext、 ControllerContext controllerContext、 ActionResult 操作结果) +56
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive (IList1 筛选器,ActionResult actionResult) +52
System.Web.Mvc.Async.<>c__DisplayClass3_6.b__4() +198
System.Web.Mvc.Async.<>c__DisplayClass3_1.b__1 (IAsyncResult 异步结果) +100
System.Web.Mvc.Async.WrappedAsyncResult1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +44
System.Web.Mvc.<>c.b__152_1 (IAsyncResult asyncResult, ExecuteCoreState innerState) +11
System.Web.Mvc.Async.WrappedAsyncVoid1.End() +49
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult,异步结果)+47
System.Web.Mvc.<>c.b__151_2(IAsyncResult asyncResult,控制器控制器) +13
System.Web.Mvc.Async.WrappedAsyncVoid1.End() +49
System.Web.Mvc.Controller.EndExecute(IAsyncResult 异步结果) +28
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.<>c.b__20_1 (IAsyncResult asyncResult, ProcessRequestState innerState) +28
System.Web.Mvc.Async.WrappedAsyncVoid1.End() +49
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult,异步结果)+28
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult 结果) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9850569
System.Web.HttpApplication.ExecuteStepImpl (IExecutionStep 步骤) +50
System.Web.HttpApplication.ExecuteStep (IExecutionStep 步骤, Boolean& completedSynchronously) +1631[VillaSite.Models.ReservationForm]'.] System.Web.Mvc.ViewDataDictionary
1.SetViewData(ViewDataDictionary viewData) +49 System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +99 System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList
1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +22 System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase
版本信息: Microsoft .NET Framework 版本:4.0.30319;ASP.NET 版本:4.8.4667.0
相关页面代码如下;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace VillaSite.Models
{
public class ReservationForm
{
public int Id { get; set; }
[Display(Name = "Ad Soyad")]
[Required]
public string Fullname { get; set; }
[Display(Name = "Telefon")]
[Required]
public string Phone { get; set; }
[Display(Name = "E-posta")]
[Required]
[EmailAddress]
public string Email { get; set; }
[Display(Name = "Villa Adı")]
[Required]
public string VillaName { get; set; }
[Display(Name = "Rezervasyon Başlangıç Tarihi")]
[Required]
public DateTime CheckInDate { get; set; }
[Display(Name = "Rezervasyon Bitiş Tarihi")]
[Required]
public DateTime CheckOutDate { get; set; }
[Display(Name = "İşlemi Tamamlandı")]
public bool IsCompleted { get; set; }
public List<ReservationForm> ReservationForms { get; set; }
}
}
public ActionResult ReservationForm(string villaNameFilter)
{
using (var db = new DataContext())
{
var reservations = db.ReservationForms.AsQueryable();
if (!string.IsNullOrEmpty(villaNameFilter))
{
reservations = reservations.Where(r => r.VillaName.Contains(villaNameFilter));
}
return View(reservations.ToList<ReservationForm>());
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ReservationForm(ReservationForm reservationform)
{
if (ModelState.IsValid)
{
try
{
// Save the reservation data to the database
using (var db = new DataContext())
{
db.ReservationForms.Add(reservationform);
db.SaveChanges();
}
// Redirect to the success page
return RedirectToAction("Success", "Home"); // Assumes "Success" is the name of the action to show the success view
}
catch (Exception ex)
{
// Log the exception (optional)
// You can use a logging framework like Serilog, NLog, or log4net to log the exception.
Console.WriteLine("Error retrieving reservations: " + ex.Message);
// Show an error message to the user
TempData["ErrorMessage"] = "Rezervasyon Tarihlerini Kayıt Ederken Bir Hata Oluştu";
}
}
// If the model state is invalid, return to the same view with validation errors
//return View(reservationform);
return View(new List<ReservationForm> { reservationform });
}
@model IEnumerable<VillaSite.Models.ReservationForm> <h2>Rezervasyon Talep Formu</h2>
@using (Html.BeginForm("ReservationForm", "AdminPanel", FormMethod.Get))
{
<div class="form-group">
@Html.Label("Villa Adı İle Listele")
@Html.TextBox("villaNameFilter", Request.QueryString["villaNameFilter"], new { @class = "form-control" })
</div>
<br />
<button type="submit" class="btn btn-primary">Filtrele</button>
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Fullname)
</th>
<th>
@Html.DisplayNameFor(model => model.Phone)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.VillaName)
</th>
<th>
@Html.DisplayNameFor(model => model.CheckInDate)
</th>
<th>
@Html.DisplayNameFor(model => model.CheckOutDate)
</th>
<th>
@Html.DisplayNameFor(model => model.IsCompleted)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Fullname)
</td>
<td>
@Html.DisplayFor(modelItem => item.Phone)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.VillaName)
</td>
<td>
@Html.DisplayFor(modelItem => item.CheckInDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.CheckOutDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsCompleted)
</td>
<td>
@Html.ActionLink("Güncelle", "ReservationsListEdit", new { id = item.Id }) |
@*@Html.ActionLink("Sil", "ReservationsListDelete", new { id=item.Id }) |*@
</td>
</tr>
}
</table>
My expectation is that the related codes work properly and the reservation request appears in the admin panel.
答: 暂无答案
评论