我正在尝试实现 ASP.NET Core MVC 应用程序,以下代码抛出错误:
“AdverseEventReport”是一个命名空间,但像类型一样使用
据我所知,这与实例化问题有关。任何可以提供的帮助将不胜感激。感谢您的帮助。
这是模型类:
namespace AdverseEventReportAPP.Models
{
public class AdverseEventReport
{
public required string Date { get; set; }
public required string PatientID { get; set; }
public required string PreExistingConditions { get; set; }
public required string AdverseSymptoms { get; set; }
public required string Severity { get; set; }
public bool ReportableToFDA { get; set; }
public required string Reasoning { get; set; }
public required string DrugName { get; set; }
public required string DrugType { get; set; }
public required string ReasonForTaking { get; set; }
}
}
这是控制器的代码:
using AdverseEventReportAPP.Models; // Ensure the correct model namespace is used
namespace AdverseEventReportApp.Controllers
{
public class AdverseEventController : Controller
{
[HttpGet]
public IActionResult Index()
{
// Correct instantiation of the AdverseEventReport model
var model = new AdverseEventReportAPP
{
Date = "2024-10-21",
PatientID = "12345",
PreExistingConditions = "None",
AdverseSymptoms = "Headache, Dizziness",
Severity = "Moderate",
ReportableToFDA = true,
Reasoning = "Critical side effect",
DrugName = "Aspirin",
DrugType = "Analgesic",
ReasonForTaking = "Pain Relief"
};
return View(model);
}
[HttpPost]
public IActionResult Submit(AdverseEventReport model)
{
if (ModelState.IsValid)
{
// Save logic here
return RedirectToAction("Index");
}
return View("Index", model);
}
}
}
这是表格的代码:
@model AdverseEventReportAPP.Models.AdverseEventReport
<form asp-action="Submit" method="post">
<table style="width: 100%;">
<tr>
<td><img src="http://localhost/AdverseEvent/images/AdverseBanner.jpg" alt="" width="508" height="67" /></td>
</tr>
</table>
<table style="width: 100%;">
<tr><td><hr /></td></tr>
</table>
<table style="width: 100%;">
<tr>
<td style="width: 20%;"><strong>Date:</strong></td>
<td style="width: 65%;"><input type="text" name="Date" value="@Model.Date" /></td>
</tr>
<tr>
<td style="width: 20%;"><strong>Patient ID:</strong></td>
<td style="width: 65%;"><input type="text" name="PatientID" value="@Model.PatientID" /></td>
</tr>
<tr>
<td style="width: 20%;"><strong>Pre-existing Conditions:</strong></td>
<td style="width: 65%;"><input type="text" name="PreExistingConditions" value="@Model.PreExistingConditions" /></td>
</tr>
<tr>
<td style="width: 20%;"><strong>Adverse Symptoms:</strong></td>
<td style="width: 65%;"><input type="text" name="AdverseSymptoms" value="@Model.AdverseSymptoms" /></td>
</tr>
<tr>
<td style="width: 20%;"><strong>Severity:</strong></td>
<td style="width: 65%;"><input type="text" name="Severity" value="@Model.Severity" /></td>
</tr>
<tr>
<td style="width: 20%;"><strong>Reportable to FDA:</strong></td>
<td style="width: 65%;"><input type="checkbox" name="ReportableToFDA" @(Model.ReportableToFDA ? "checked" : "") /></td>
</tr>
<tr>
<td style="width: 20%;"><strong>Reasoning:</strong></td>
<td style="width: 65%;"><input type="text" name="Reasoning" value="@Model.Reasoning" /></td>
</tr>
</table>
<table style="width: 100%;">
<tr><td><hr /></td></tr>
</table>
<table style="width: 100%;">
<tr>
<td style="width: 20%;"><strong>Drug Name:</strong></td>
<td style="width: 65%;"><input type="text" name="DrugName" value="@Model.DrugName" /></td>
</tr>
<tr>
<td style="width: 20%;"><strong>Drug Type:</strong></td>
<td style="width: 65%;"><input type="text" name="DrugType" value="@Model.DrugType" /></td>
</tr>
<tr>
<td style="width: 20%;"><strong>Reason for taking Medication:</strong></td>
<td style="width: 65%;"><input type="text" name="ReasonForTaking" value="@Model.ReasonForTaking" /></td>
</tr>
</table>
<table style="width: 100%;">
<tr><td><hr /></td></tr>
</table>
<table style="width: 100%;">
<tr>
<td><img src="http://localhost/AdverseEvent/images/Blue_Prism_AWS_Partnership.jpg" alt="" width="320" height="104" /></td>
</tr>
</table>
<button type="submit">Submit</button>
</form>
如果存在命名空间冲突,则可以向命名空间添加别名。示例:
using MyAlias = AdverseEventReportAPP.Models;
namespace AdverseEventReportApp.Controllers
{
[HttpPost]
public IActionResult Submit(MyAlias.AdverseEventReport model)
{
if (ModelState.IsValid)
{
// Save logic here
return RedirectToAction("Index");
}
return View("Index", model);
}
}
对于视图:
@using MyAdverseEventReport = AdverseEventReportAPP.Models.AdverseEventReport
...
但我建议检查整个项目的命名空间约定......