Controller:
        public ActionResult Create()
        {
            LisknoveDataContext db = new LisknoveDataContext();
            this.ViewBag.genrelist = new SelectList(db.Genres, "genreId", "genreName");
            this.ViewBag.materiallist = new SelectList(db.Materials, "materialId", "materialName");

            return View();
        }
        public string filenames;
        [HttpPost]
        public ActionResult AddRing(Ring ring)
        {
            try
            {
                foreach (string inputTagName in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[inputTagName];
                    var filepath= Path.Combine(HttpContext.Server.MapPath("/content/p_w_picpaths"), Path.GetFileName(file.FileName));
                    file.SaveAs(filepath);
                    filenames = file.FileName;
                };
                

                ring.ringName = Request.Form["ringName"];
                ring.ringImage = filenames;
                ring.price = Convert.ToDecimal(Request.Form["price"]);
                ring.num = Convert.ToInt32(Request.Form["num"]);
                ring.genreId = Convert.ToInt32(Request.Form["genrelist"]);
                ring.materialId = Convert.ToInt32(Request.Form["materiallist"]);

                ringRepository.Add(ring);
                ringRepository.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                LisknoveDataContext db = new LisknoveDataContext();
                this.ViewBag.genrelist = new SelectList(db.Genres, "genreId", "genreName");
                this.ViewBag.materiallist = new SelectList(db.Materials, "materialId", "materialName");
                return View();
            }
        }
View:
@using (Html.BeginForm("AddRing", "Ring", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Ring</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.ringName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ringName)
            @Html.ValidationMessageFor(model => model.ringName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ringImage)
        </div>
        <div class="editor-field">
            <input type="file" id="uploadimg" name="uploadimg" value="UploadImage"/>
            @Html.ValidationMessageFor(model => model.ringImage)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.price)
            @Html.ValidationMessageFor(model => model.price)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.num)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.num)
            @Html.ValidationMessageFor(model => model.num)
        </div>

        <div class="editor-label">
            @Html.Label("GenreName")
        </div>
        <div class="editor-field">
            @Html.DropDownList("genrelist")
            @Html.ValidationMessageFor(model => model.genreId)
        </div>

        <div class="editor-label">
             @Html.Label("MaterialName")
        </div>
        <div class="editor-field">
            @Html.DropDownList("materiallist")
            @Html.ValidationMessageFor(model => model.materialId)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

2011年06月22日 - Lisknove - Lisknove