MVC表单数据子目录然后保存到数据库

时间:2016-04-16 06:00:35

标签: asp.net asp.net-mvc asp.net-mvc-4

模型

public partial class portfolio
{
    public int portfolio_id { get; set; }
    public int proj_id { get; set; }
    public string title { get; set; }
    public string image { get; set; }
    public string url { get; set; }
    public string description { get; set; }

    public virtual project project { get; set; }
}        

表格,我将数据和传递给控制器​​

<h2>Create View<h2>
@using(Html.BeginForm("Create","Portfolio",FormMethod.Post,new{enctype="multipart/form-data",id="profilepicture"})) 
{

    <div class="form-horizontal">
    <h4>Portfolio</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
        @Html.Label("Title", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-4">

            @Html.TextBox("Name",(String)ViewBag.Name,new {@class="form-control",@disabled="true" })

             @Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.Label("Description", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            @Html.EditorFor(model => model.description, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.description, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.Label("URL", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            @Html.TextBox("Url",(String)ViewBag.Url, new  { @class = "form-control",@disabled="true"  })
            @Html.ValidationMessageFor(model => model.url, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.Label("Image", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            <input type="file" name="File" id="File"/>
            @*@Html.TextBoxFor(model => model.image,new { type = "file" })*@
            <input type="submit" value="Upload" class="submit" /> 
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-4">
            <input type="submit" value="Add To Portfolio" class="btn btn-info" />
        </div>
    </div>
</div>

我必须发布数据的控制器动作

public ActionResult Create(portfolio por)
    {

        //long unique = DateTime.Now.Ticks;
        //int counter = 0;
        //foreach (string fcName in Request.Files)
        //{
        //    HttpPostedFileBase file = Request.Files[fcName];
        //    string temp = unique + "_" + ++counter + file.FileName.Substring(file.FileName.LastIndexOf("."));
        //    string url = "~/Content/images" + temp;
        //    file.SaveAs(Request.MapPath(url));
        //    por.image= url;


        //}
        if(por.image.content)
        bool result = _DaoPortfolio.AddProject(por);
        if (result == true)
        {
            return RedirectToAction("Index", "Employee");
        }
        else
            return View();


      }

1 个答案:

答案 0 :(得分:0)

我不知道你面对的是什么问题,但试试这个...它为我工作

此处WorkType是我的模型

公共部分类WorkType

{
    public WorkType()
    {
        this.Works = new HashSet<Work>();
    }

    public int ID { get; set; }
    public string Type { get; set; }

    public virtual ICollection<Work> Works { get; set; }
}

控制器:

[HttpPost]

    public ActionResult Create(FormCollection collection)
    {
        try
        {
            if (ModelState.IsValid)
            {
                WorkType worktype = new WorkType();
                worktype.Type = collection["Type"].ToString();

                db.Entry(worktype).State = System.Data.EntityState.Added;
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

AND视图:

@using(Html.BeginForm())

{

@ Html.AntiForgeryToken()

@Html.ValidationSummary(true)


<fieldset>
    <legend>WorkType</legend>

    <div class="editor-label">
        @Html.Label("Work Type")
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Type)
        @Html.ValidationMessageFor(model => model.Type)
    </div>

    <div class="span7">
        <input type="submit" value="Create" class="span2 btn btn-default" />
        <button type="button" onclick="location.href='@Url.Action("Index", "Slider")'" class="btn btn-primary">
            Back to List
        </button>
    </div>
</fieldset>

}