C# 如何在MVC asp.net core+;sql server上的更新?
我想上传MVC.net core 2中的文件,我是学生,对这项技术还不熟悉(我在这里读了一些问答,但没有回答这个问题) 我有一个产品模型的添加操作和视图。在视图中,有一个表单用于创建具有某些属性的产品。我还想添加一个上传图像的选项-单击add按钮后,需要首先使用实体框架代码将产品保存在数据库中 我不使用ViewModel(我也不想用它来解决问题),但也许以后我会想为图像实体创建一个类。现在不行(对我来说太多了) 请帮我怎么做。这是代码 型号-产品:C# 如何在MVC asp.net core+;sql server上的更新?,c#,asp.net-mvc,model-view-controller,C#,Asp.net Mvc,Model View Controller,我想上传MVC.net core 2中的文件,我是学生,对这项技术还不熟悉(我在这里读了一些问答,但没有回答这个问题) 我有一个产品模型的添加操作和视图。在视图中,有一个表单用于创建具有某些属性的产品。我还想添加一个上传图像的选项-单击add按钮后,需要首先使用实体框架代码将产品保存在数据库中 我不使用ViewModel(我也不想用它来解决问题),但也许以后我会想为图像实体创建一个类。现在不行(对我来说太多了) 请帮我怎么做。这是代码 型号-产品: public class Product
public class Product
{
[Key]
public Guid Id { get; set; }
[Required]
[StringLength(50, MinimumLength = 3)]
public string Name { get; set; }
[Required]
public string Description { get; set; }
[Required]
public string Category { get; set; }
[Required]
public string Gender { get; set; }
[Required]
public string Age { get; set; }
[Required]
public bool ConditionIsNew { get; set; }
}
控制器-产品控制器
public class ProductController : Controller
{
[HttpGet]
public IActionResult Add()
{
return View(new Product());
}
[HttpPost]
public IActionResult Add(Product p)
{
if (ModelState.IsValid)
{
ProductDal dal = new ProductDal();
dal.Products.Add(p);
dal.SaveChanges();
return View("Show", p);
}
return View(p);
}
}
视图-添加(Razor语法、标记帮助器)
@model MyProjectTest.Models.Product
@{
Layout=“~/Shared/_Layout.cshtml”;
}
提交
添加产品
同样,我没有ViewModel,也不想使用它
我还想知道如何将图像更新到数据库
谢谢
及
有一件事你需要知道。输入将包含上传文件的二进制表示,所以如果您想以某种方式使用它,您需要记住对其进行反序列化/序列化
及
有一件事你需要知道。输入将包含上传文件的二进制表示,所以如果您想以某种方式使用它,您需要记住对其进行反序列化/序列化 Html层:
*输入type=“file”name=“fileABC”/>
控制器层:
方法html post,参数为HttpPostedFileBase类型并命名为fileABC(必须由
与本例中型号的参数分开(产品型号)
对于SQL:
使用blob、filestream…Html层:
*输入type=“file”name=“fileABC”/>
控制器层:
方法html post,参数为HttpPostedFileBase类型并命名为fileABC(必须由
与本例中型号的参数分开(产品型号)
对于SQL:
使用blob,filestream…Hi@Robert,我知道如何制作输入标记。但是谢谢:)你发给我的剃须刀页面不是我需要的,这和MVC不一样。我也读了微软的文档,但是我的问题没有答案。这是文档Hi@Robert,我知道如何制作输入标签。但是谢谢:)你发给我的剃须刀页面不是我需要的,这和MVC不一样。我也读了微软的文档,但是我的问题没有答案。以下是要将图像上载到sql server的文档。我说的对吗?文档中已经记录了这一点:。是否要在数据库中保存url,还是确实要在数据库中保存图像?是否要将图像上载到sql server。我说的对吗?文档中已经记录了这一点:。您是想在数据库中保存url,还是真的想在数据库中保存图像?
@model MyProjectTest.Models.Product
@{
Layout = "~/Shared/_Layout.cshtml";
}
<h1>Submit</h1>
<h2>Add product</h2>
<form asp-controller="Product" asp-action="Add" method="post">
<label asp-for="Name"></label> <input asp-for="Name" /> <span asp-validation-for="Name"></span>
<br />
<label asp-for="Description"></label> <input asp-for="Description" /> <span asp-validation-for="Description"></span>
<br />
<label asp-for="Category"></label> <input asp-for="Category" /> <span asp-validation-for="Category"></span>
<br />
<label asp-for="Gender"></label> <input asp-for="Gender" /> <span asp-validation-for="Gender"></span>
<br />
<label asp-for="Age"></label> <input asp-for="Age" /> <span asp-validation-for="Age"></span>
<br />
<label asp-for="ConditionIsNew"></label> <input asp-for="ConditionIsNew" /> <span asp-validation-for="ConditionIsNew"></span>
<br />
<input id="SubmitProduct" type="submit" value="Enter" />
</form>