C# 将接口从视图传递到控制器
我一直在试图找出如何通过视图中的表单填充特定的接口。该接口位于与my controller/view不同的项目和命名空间中,并自动生成,用于在数据库中存储数据: 接口名称空间和代码:C# 将接口从视图传递到控制器,c#,asp.net-mvc,C#,Asp.net Mvc,我一直在试图找出如何通过视图中的表单填充特定的接口。该接口位于与my controller/view不同的项目和命名空间中,并自动生成,用于在数据库中存储数据: 接口名称空间和代码: DataAccess.DAL.IVehicle namespace DataAccess.DAL { public partial interface IVehicle { String vehicleName { get; set; } int maxSpeed
DataAccess.DAL.IVehicle
namespace DataAccess.DAL
{
public partial interface IVehicle
{
String vehicleName { get; set; }
int maxSpeed { get; set; }
}
}
namespace coreproject.Controllers
{
public class NewVehicleController
{
[HttpPost, ValidateInput(false)]
public JsonResult AddVechicle(IVehicle newVehicle)
{
// I expect that newVechicle is populated via the form
}
}
}
<%
// This is not working, I am not sure how to tell the view I want the form
// to use the interface located in the following namespace.
@Model DataAccess.DAL.IVehicle;
using (Html.BeginForm("AddVehicle", "NewVechicle", FormMethod.Post))
// Below I understand that I would need some code in the form of Html.EditorFor to
// populate the IVehicle interface in the form. I have seen this as an example:
<%: Html.EditorFor(model => model.VehicleName) %>
<%: Html.EditorFor(model => model.maxSpeed) %>
<%:
}
%>
我有一个控制器,它有一个从视图中的窗体接收信息的操作方法:
控制器代码:
DataAccess.DAL.IVehicle
namespace DataAccess.DAL
{
public partial interface IVehicle
{
String vehicleName { get; set; }
int maxSpeed { get; set; }
}
}
namespace coreproject.Controllers
{
public class NewVehicleController
{
[HttpPost, ValidateInput(false)]
public JsonResult AddVechicle(IVehicle newVehicle)
{
// I expect that newVechicle is populated via the form
}
}
}
<%
// This is not working, I am not sure how to tell the view I want the form
// to use the interface located in the following namespace.
@Model DataAccess.DAL.IVehicle;
using (Html.BeginForm("AddVehicle", "NewVechicle", FormMethod.Post))
// Below I understand that I would need some code in the form of Html.EditorFor to
// populate the IVehicle interface in the form. I have seen this as an example:
<%: Html.EditorFor(model => model.VehicleName) %>
<%: Html.EditorFor(model => model.maxSpeed) %>
<%:
}
%>
我知道我应该在视图中使用Html.BeginForm。下面是我提出的一些代码,我知道视图中需要这些代码
查看代码:
DataAccess.DAL.IVehicle
namespace DataAccess.DAL
{
public partial interface IVehicle
{
String vehicleName { get; set; }
int maxSpeed { get; set; }
}
}
namespace coreproject.Controllers
{
public class NewVehicleController
{
[HttpPost, ValidateInput(false)]
public JsonResult AddVechicle(IVehicle newVehicle)
{
// I expect that newVechicle is populated via the form
}
}
}
<%
// This is not working, I am not sure how to tell the view I want the form
// to use the interface located in the following namespace.
@Model DataAccess.DAL.IVehicle;
using (Html.BeginForm("AddVehicle", "NewVechicle", FormMethod.Post))
// Below I understand that I would need some code in the form of Html.EditorFor to
// populate the IVehicle interface in the form. I have seen this as an example:
<%: Html.EditorFor(model => model.VehicleName) %>
<%: Html.EditorFor(model => model.maxSpeed) %>
<%:
}
%>
型号(最大速度)%%>
我提出的问题有两个,与观点有关:
任何帮助都将不胜感激。您在这里混合了许多概念
[HttpGet]
public ActionResult AddVechicle()
{
return View(new Vehicle());
}
[HttpPost, ValidateInput(false)]
public JsonResult AddVechicle(Vehicle newVehicle)
{
// I expect that newVechicle is populated via the form
}
然后,您可以按如下方式声明“Vehicle”类
public class Vehicle :IVehicle
{
String vehicleName { get; set; }
int maxSpeed { get; set; }
}
我还没有测试视图是否接受接口作为模型,您最好将其更改为类“Vehicle”
型号(最大速度)%%>
我知道接口通常用作抽象类型和多态性(依赖注入)。我在发帖之前就知道了。我只是想看看如何通过表单将界面传递给控制器。我已经解决了发布表单时如何从视图中找到控制器的问题。现在我唯一的问题是将接口发送到控制器。答案已更新。。。为什么你想要一个“JsonResult”,它似乎是错的。。。请发布完整的源代码。这里发布的代码,减去视图文件中的内容,都是从一个功能项目中摘录的,当然还有修改过的类/接口名称。我想指出的是,在你建议的回答中,你没有发布任何代码,说明观点需要什么才能使你的建议起作用。这种“你不能那样做!”的方法,在你建议的答案中漏掉了关键信息,这并不完全是我认为有用的。请随时更新您的答案,包括完整的代码,该视图将需要,如果它在我这方面的工作,我会接受它。对不起,如果我有点不礼貌,但由于你没有提供一个完整的样本,我不能提供一个完整的解决方案。如果你在一个动作中使用一个接口,你会得到一个异常,说它不能创建一个抽象类。因此,“你不能那样做!”你缺少控制器的“GET”部分,这是项目的完整来源吗?