根据我对此问题的理解,我已经在网上尝试了很多可用的解决方案但是在MVC中是新的,我仍然无法找到解决方案。请帮助。
我有一个视图,该网站的主页名为“Index.cshtml”,位于以下路径下: 的 WebsiteName /地区/网站/查看/ CypressHome / Index.cshtml
其次,我创建了一个用户试用表格作为名为“_ PartialHomeFormFreeTrial.cshtml”的部分视图,它位于以下路径下: 的 WebsiteName /地区/网站/共享/ _PartialHomeFormFreeTrial.cshtml 即可。 我在“Index.cshtml”中使用的这个表单如下:
<!--freetrialform-->
@Html.Partial("_PartialHomeFormFreeTrial")
<!--//freetrialform-->
现在,我的部分页面是发布包含以下元素的数据:
@using (Html.BeginForm("Create", "Customer", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div>
@Html.EditorFor(model => model.CustomerName, new { htmlAttributes = new { @class = "input__field input__field--kuro" } })
@Html.ValidationMessageFor(model => model.CustomerName, "", new { @class = "text-danger" })
............
other fields such as email, phone, date, etc..
<input type="submit" id="" value="SEND REQUEST" />
</div>
}
现在,我已经创建了一个名为“CustomerController”的控制器,该控制器具有以下代码,用于将表单数据保存为用作部分视图在主视图中“Index.cshtml” :
public class CustomerController : Controller
{
private WebsiteContext db = new WebsiteContext();
// GET: Website/Customer
public ActionResult Index()
{
return View();
}
// GET: Website/Customer/Create
public ActionResult Create()
{
ViewBag.StatusPlanID = new SelectList(db.StatusPlans, "StatusPlanID", "Status");
return View("Index");
}
// POST: Website/Customer/Create
[HttpPost]
public ActionResult Create([Bind(Include = "CustomerID,CustomerName,CustomerEmail,CustomerPhone,DateOfRegistration,StatusPlanID")] Customer customer)
{
if (ModelState.IsValid)
{
db.Customers.Add(customer);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.StatusPlanID = new SelectList(db.StatusPlans, "StatusPlanID", "Status", customer.StatusPlanID);
return View(customer);
}
}
我在我的控制器中尝试了很多更改,返回视图和 许多其他的事情,但我总是得到同样的错误。不是 验证正在运行,验证的数据也未保存。
错误如下:
Server Error in '/' Application.
The view 'Create' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Areas/Website/Views/Customer/Create.aspx
~/Areas/Website/Views/Customer/Create.ascx
~/Areas/Website/Views/Shared/Create.aspx
~/Areas/Website/Views/Shared/Create.ascx
~/Views/Customer/Create.aspx
~/Views/Customer/Create.ascx
~/Views/Shared/Create.aspx
~/Views/Shared/Create.ascx
~/Areas/Website/Views/Customer/Create.cshtml
~/Areas/Website/Views/Customer/Create.vbhtml
~/Areas/Website/Views/Shared/Create.cshtml
~/Areas/Website/Views/Shared/Create.vbhtml
~/Views/Customer/Create.cshtml
~/Views/Customer/Create.vbhtml
~/Views/Shared/Create.cshtml
~/Views/Shared/Create.vbhtml
网址正在变化如下: 1.最初运行系统时:http://localhost:53872/ 2.点击提交:http://localhost:53872/Areas/Website/Customer/Create以及 错误如上所述。
有关详细信息,我的WebsiteAreaRegistration.cs文件包含以下代码:
public class WebsiteAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Website";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Website_home",
"",
new { controller = "CypressHome", action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
"Website_default",
"Areas/Website/{controller}/{action}/{id}",
new { controller = "CypressHome", action = "Index", id = UrlParameter.Optional }
);
}
}
虽然我已经理解了这个问题但却无法理解。请帮助。
答案 0 :(得分:1)
在您的代码中,最后一个返回语句为return View(customer)
。这意味着在 POST 数据之后,它返回一个View(使用HTTP GET方法),其名称与 Action 的名称相同,即创建。但是您的描述中有创建操作,但您没有查看页面。
因此,请使用与客户对象相对应的模型创建Create.cshtml
。
或者更改return语句。
根据您的评论,您可以按照此更改进行操作。
1.去除
public ActionResult Create()
{
ViewBag.StatusPlanID = new SelectList(db.StatusPlans, "StatusPlanID", "Status");
return View("Index");
}
2.然后改变
public ActionResult Index()
{
ViewBag.StatusPlanID = new SelectList(db.StatusPlans, "StatusPlanID", "Status");
return View(new Customer());
}
3.in Index.cshtml
@Model Customer
@Html.Partial("_PartialHomeFormFreeTrial",Model)
4.然后
return View("Index",customer);