Asp.net mvc MVC:从表(HasTags)中填充我表单中的许多复选框,并将它们保存为id';在我的第一张桌子的一个区域内(照片)
我有一个名为Creations的表,在这个表中我添加了一个名为Creationtag的字段。 在上传照片的过程中,我需要在表单中填充来自表HairTag的许多复选框,如果用户选中其中一些复选框,请在我的控制器中,将它们保存在我的字段Creationtag中 它看起来像这样:Asp.net mvc MVC:从表(HasTags)中填充我表单中的许多复选框,并将它们保存为id';在我的第一张桌子的一个区域内(照片),asp.net-mvc,Asp.net Mvc,我有一个名为Creations的表,在这个表中我添加了一个名为Creationtag的字段。 在上传照片的过程中,我需要在表单中填充来自表HairTag的许多复选框,如果用户选中其中一些复选框,请在我的控制器中,将它们保存在我的字段Creationtag中 它看起来像这样: 1001,1008,1012,1015 你能帮我做到这一点吗?我是mvc的初学者 这是my model Creation.cs: using System; using System.Collections.Generic
1001,1008,1012,1015
你能帮我做到这一点吗?我是mvc的初学者
这是my model Creation.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace HairCollection3.Models
{
public class Creation
{
public string UserId { get; set; }
[Key]
public int CreationId { get; set; }
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ViewRes.ValidationStrings))]
[Display(Name = "CreationTitle", ResourceType = typeof(ViewRes.NamesCreation))]
[StringLength(2000)]
[AllowHtml]
public string CreationTitle { get; set; }
public string CreationPhotoBis { get; set; }
public string Creationtag { get; set; }
public virtual ICollection<CreationLike> CreationLikes { get; set; }
}
public class CreationLike
{
public int CreationId { get; set; }
public string UserId { get; set; }
public int CreationLikeId { get; set; }
public virtual Creation ParentCreation { get; set; }
}
public class HairTag
{
[Key]
public int HairTagId { get; set; }
[Required]
public string HairTagTitle { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
名称空间集合3.Models
{
公共类创建
{
公共字符串用户标识{get;set;}
[关键]
public int CreationId{get;set;}
[必需(ErrorMessageResourceName=“必需”,ErrorMessageResourceType=typeof(ViewRes.validationString))]
[显示(Name=“CreationTitle”,ResourceType=typeof(ViewRes.NamesCreation))]
[秘书长(2000年)]
[allowtml]
公共字符串CreationTitle{get;set;}
公共字符串CreationPhotoBis{get;set;}
公共字符串Creationtag{get;set;}
公共虚拟ICollection CreationLikes{get;set;}
}
公共类创造性
{
public int CreationId{get;set;}
公共字符串用户标识{get;set;}
public int CreationLikeId{get;set;}
公共虚拟创建父创建{get;set;}
}
公共级发签
{
[关键]
public int HairTagId{get;set;}
[必需]
公共字符串HairTagTitle{get;set;}
}
}
这是我的控制器CreationController.cs(创建表单)
//获取:/Creation/CreationUpload
[授权]
公共行动结果CreationUpload()
{
返回视图();
}
//POST:/Creation/CreationUpload
//Afin de D Joujouer-Les附件,PAR SURR验证,Actuvz LES PREPRIQUICS QueVouvouLEZ LIER。倒
//再加上de détails,voirhttp://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[授权]
[ValidateAntiForgeryToken]
public ActionResult CreationUpload([Bind(Include=“CreationId,CreationTitle,CreationPhotoBis”)]创建,IEnumerable文件)
{
if(ModelState.IsValid)
{
//手动更新每个字段
foreach(文件中的var文件)
{
如果(文件!=null)
{
如果(file.ContentLength>0)
{
var myuploadextension=Path.GetExtension(file.FileName);
如果(myuploadextension.ToUpper()==”.JPEG“| | myuploadextension.ToUpper()==”.JPG“| | myuploadextension.ToUpper()==”.BMP“| | myuploadextension.ToUpper()==”.PNG“| myuploadextension.ToUpper()=”.GIF”)
{
..代码上传隐藏。。。。
var fileName=todaydatefriendly+“-”+UserId+“-”+myuploadiwthoutextensionfriendly+myuploadextension;
var path=path.Combine(Server.MapPath(“~/Content/UserCreations”),文件名);
file.SaveAs(路径);
WebImage img=新的WebImage(路径);
creation.CreationTitle=creation.CreationTitle;
db.Creations.Add(创建);
db.SaveChanges();
}
}
}
}
//用户ID
返回RedirectToAction(“CreationList”,“Creation”,new{UserId=User.Identity.GetUserId()});
}
返回视图(创建);
}
这是我的视图CreationUpload.cshtml(创建表单)
@model HairCollection3.Models.Creation
@使用Microsoft.AspNet.Identity
@{
ViewBag.Title=ViewRes.NamesCreation.CreationUploadTitle;
}
@ViewRes.Shared.PublishAPhoto
@使用(Html.BeginForm(“CreationUpload”、“Creation”、FormMethod.Post、new{id=“CreationUpload”、enctype=“multipart/form data”、onsubmit=“$('#creationload').show();$('#creationform').hide()”)
{
@Html.AntiForgeryToken()
@ViewRes.Shared.UploadPhotoProgress0%
@Html.ValidationMessageFor(m=>m.CreationTitle)
@Html.TextBoxFor(m=>m.CreationTitle,new{@class=“InputPlaceholder=”,placeholder=HttpUtility.HtmlDecode(Html.DisplayNameFor(m=>m.CreationTitle.ToHtmlString()),onfocus=“this.placeholder=”,onblur=“this.placeholder=”+HttpUtility.HtmlDecode(Html.DisplayNameFor(m=>m.CreationTitle).ToHtmlString())+“})
}
为了帮助您,您必须向我们展示您尝试过的内容、控制器代码、视图代码、工作和不工作的内容。否则这个问题太宽泛了我添加了代码。。谢谢你的收看。。。我是一个乞丐,为此我挣扎着。。
// GET: /Creation/CreationUpload
[Authorize]
public ActionResult CreationUpload()
{
return View();
}
// POST: /Creation/CreationUpload
// Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
// plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult CreationUpload([Bind(Include = "CreationId,CreationTitle,CreationPhotoBis")] Creation creation, IEnumerable<HttpPostedFileBase> files)
{
if (ModelState.IsValid)
{
// update each field manually
foreach (var file in files)
{
if (file != null)
{
if (file.ContentLength > 0)
{
var myuploadextension = Path.GetExtension(file.FileName);
if (myuploadextension.ToUpper() == ".JPEG" || myuploadextension.ToUpper() == ".JPG" || myuploadextension.ToUpper() == ".BMP" || myuploadextension.ToUpper() == ".PNG" || myuploadextension.ToUpper() == ".GIF")
{
....code upload hidden ....
var fileName = todaydatefriendly + "-" + UserId + "-" + myuploadiwthoutextensionfriendly + myuploadextension;
var path = Path.Combine(Server.MapPath("~/Content/UserCreations"), fileName);
file.SaveAs(path);
WebImage img = new WebImage(path);
creation.CreationTitle = creation.CreationTitle;
db.Creations.Add(creation);
db.SaveChanges();
}
}
}
}
//UserId
return RedirectToAction("CreationList", "Creation", new { UserId = User.Identity.GetUserId() });
}
return View(creation);
}
@model HairCollection3.Models.Creation
@using Microsoft.AspNet.Identity
@{
ViewBag.Title = ViewRes.NamesCreation.CreationUploadTitle;
}
<div class="col-sm-12 col-md-12 chpagetop">
<h1>@ViewRes.Shared.PublishAPhoto</h1>
<hr />
@using (Html.BeginForm("CreationUpload", "Creation", FormMethod.Post, new { id = "CreationUpload", enctype = "multipart/form-data", onsubmit = "$('#creationloading').show(); $('#creationform').hide();" }))
{
@Html.AntiForgeryToken()
<div class="col-md-12" id="creationloading" style="display:none">
<div id="progress">
<p>@ViewRes.Shared.UploadPhotoProgress<strong>0%</strong></p>
<progress value="5" min="0" max="100"><span></span></progress>
</div>
</div>
<div class="col-md-12" id="creationform">
<div class="col-md-12">
@Html.ValidationMessageFor(m => m.CreationTitle)
@Html.TextBoxFor(m => m.CreationTitle, new { @class = "inputplaceholderviolet wid100x100", placeholder = HttpUtility.HtmlDecode(Html.DisplayNameFor(m => m.CreationTitle).ToHtmlString()), onfocus = "this.placeholder = ''", onblur = "this.placeholder = '" + HttpUtility.HtmlDecode(Html.DisplayNameFor(m => m.CreationTitle).ToHtmlString()) + "'" })
</div>
<div class="col-md-12" style="text-align: center">
<p style="display: inline-block">
<input type="file" accept="image/*" onchange="loadFile(event)" name="files" id="file1" translate="yes" data-val="true" data-val-required="A File is required." class="wid100x100" /><label for="file1"></label>
<img id="output" style="max-width:200px;"/>
</p>
</div>
<div class="col-sm-12 col-md-12 chpagetopdiv">
<button type="submit" title="@ViewRes.Shared.Publish"><span class="glyphicon glyphicon-share-alt"></span> @ViewRes.Shared.Publish</button>
</div>
</div>
}
</div>
@section Scripts{
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
function modifValues() {
var val = $('#progress progress').attr('value');
if (val >= 100) { val = 5; }
var newVal = val * 1 + 0.25;
var txt = Math.floor(newVal) + '%';
$('#progress progress').attr('value', newVal).text(txt);
$('#progress strong').html(txt);
}
setInterval(function () { modifValues(); }, 200);
</script>
<script>
var loadFile = function(event) {
var reader = new FileReader();
reader.onload = function(){
var output = document.getElementById('output');
output.src = reader.result;
};
reader.readAsDataURL(event.target.files[0]);
};
</script>
}