模型是用户:
public class Users
{
[Key]
public int UserId { get; set; }
[Required]
public string Firstname { get; set; }
public string Lastname { get; set; }
[Required]
public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
public string UserImage { get; set; }
}
在用户控制器中创建ActionMethod:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "UserId,Firstname,Lastname,Username,Password,UserImage")] Users users)
{
if (ModelState.IsValid)
{
db.Users.Add(users);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(users);
}
最后创建视图就像这样:
@model WebApplication2.Models.Users
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Users</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Firstname, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Firstname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Firstname, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Lastname, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Lastname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Lastname, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Username, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Username, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserImage, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="UserImage" value="" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
所以PLZ帮助我怎么做我怎么用这个用户对象将图像名称传递给数据库我已经尝试了很多但是失败了。
答案 0 :(得分:1)
不要使用EF中的实体类在视图和操作方法之间传输数据。你应该创建一个视图模型。您可以为类型为{
try{
return future.get();
}
catch (InterruptedException | ExecutionException e){
e.printStackTrace();
throw new RuntimeException(e)
}
}
HttpPostedFileBase
现在,在您的GET中,请确保发送此CreateUserVm类的对象。
public class CreateUserVm
{
public int UserId { get; set; }
[Required]
public string Firstname { get; set; }
public string Lastname { get; set; }
[Required]
public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
public HttpPostedFileBase UserImage { get; set; }
}
并且您的创建视图将强烈输入此视图模型。
public ActionResult Create()
{
return View(new CreateUserVm());
}
在你的HttpPost动作中,它接受我们的CreateUserVm视图模型的对象作为参数。您可以使用唯一名称将文件保存在磁盘中,并将其与用户记录关联,以便以后检索。
@model YourNameSpaceHere.CreateUserVm
@using (Html.BeginForm("Create", "Home", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(false)
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Name,
"", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserImage, new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="UserImage" />
</div>
</div>
<input type="submit" />
}
答案 1 :(得分:1)
好的,你有一些事情可以做这个工作。首先,您需要更改视图中的表单类型,以向服务器指示文件正在进行中:
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
//Form Stuff here
}
然后,在控制器端,您需要通过添加UserImage
类型的参数HttpPostedFileBase
来告诉服务器期望发布请求中的文件:
public ActionResul Upload([Bind(Include = "UserId,Firstname,Lastname,Username,Password")] Users users, HttpPostedFileBase UserImage)
{
//Controller Logic
}
现在,您的视图正在发送图像,控制器正在接受它......现在我们需要解决在数据库中存储图像的概念。您可以通过将上传的图像(现在存储在UserImage
变量中)转换为字节数组(此过程未在我的帖子but is described here中显示)来更改omniauth-facebook cannnot get email address中的属性。 1}}模型来自:
Users
以下内容:
public string UserImage { get; set; }
但是,我建议你不要这样做有几个原因......相反,我会建议你做以下事情:
public byte[] UserImage { get; set; }
将检索图像yoursite.com/Users/ProfileImage/{UserID}
,它将加载正确的用户图像。我不打算概述上述工作流程的每一步,但通常是如何处理您描述的用例。