ASP.NET MVC5正在上传图片

时间:2015-05-12 15:13:50

标签: c# asp.net-mvc

我的问题是我创建了一个网页,我可以在其中上传图像并创建新的配方(填充文本框),但提交按钮不执行任何操作。任何解决方案?

    // GET: Recipe/Create
    public ActionResult Create()
    {
        var userId = User.Identity.GetUserId();
        var viewModel = new ReceptDTO
        {
            Date = DateTime.Now,
            UserId = userId
        };
        return View(viewModel);
    }

    // POST: Recipe/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(ReceptDTO recipe, HttpPostedFileBase file)
    {
        if (ModelState.IsValid)
        {
            ReceptManager recipes = new ReceptManager();
            recipes.SaveRecept(recipe);
            if (file != null)
            {
                string pic = System.IO.Path.GetFileName(file.FileName);
                string path = System.IO.Path.Combine(
                       Server.MapPath("~/images/recipes/" + User.Identity.GetUserId()), pic);
                // File uploaded
                file.SaveAs(path);
            }

            return RedirectToAction("Index", "Home");
        }

        return View(recipe);
    }

我的控制器看起来像这样:

        @model Receptura_v4.Models.ReceptDTO

        @{
            ViewBag.Title = "Új recept feltöltése";
        }

        <h2>Új recept feltöltése</h2>

        @using (Html.BeginForm("Create", "Recipe", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
@Html.AntiForgeryToken()

<label for="photo">Fotó feltöltése:</label>
<input type="file" name="file1" id="file1" style="width: 100%;" />


<div class="form-horizontal">
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.Label("Cím:", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.Label("Adag", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="col-md-3">
                @Html.TextBoxFor(model => model.Portion, new { id = "portion", @readonly = "readonly", style = "border:0; color:#f6931f; font-weight:bold;" })
                <div id="slider-range-min"></div>
            </div>
        </div>
    </div>

    <div class="form-group">
        @Html.Label("Elkészítési idő", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="col-md-3">
                @Html.TextBoxFor(model => model.Time, new { id = "time", @readonly = "readonly", style = "border:0; color:#f6931f; font-weight:bold;" })
                <div id="slider-range-min2"></div>
            </div>
        </div>
    </div>
    @*<div class="form-group">
    @Html.Label("Elkészítési idő", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.Time, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Time, "", new { @class = "text-danger" })
    </div>
    </div>*@

    <div class="form-group">
        @Html.Label("Kategória", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("Category", new List<SelectListItem>
            {
            new SelectListItem { Text="Levesek", Value = "Levesek"},
            new SelectListItem { Text="Főzelékek", Value = "Főzelékek"},
            new SelectListItem { Text="Desszertek", Value = "Desszertek"},
            new SelectListItem { Text="Egytálételek", Value = "Egytálételek"}
            }, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.Label("Elkészítés", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Preparation, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Preparation, "", new { @class = "text-danger" })
        </div>
    </div>


    <div class="form-group">
        @Html.Label("Nehézség", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="col-md-3">
                @Html.TextBoxFor(model => model.Difficulty, new { id = "amount", @readonly = "readonly", style = "border:0; color:#f6931f; font-weight:bold;" })
                <div id="slider"></div>
            </div>
        </div>
    </div>

    @*<div class="form-group">
    @Html.Label("Elkészítési költség", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
    </div>
    </div>*@

    <div class="form-group">
        @Html.Label("Elkészítési költség", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="col-md-3">
                @Html.TextBoxFor(model => model.Price, new { id = "price", @readonly = "readonly", style = "border:0; color:#f6931f; font-weight:bold;" })
                <div id="slider-range-min3"></div>
            </div>
        </div>
    </div>

    @Html.HiddenFor(model => model.Date)
    @Html.HiddenFor(model => model.UserId)
    <div class="form-group">
        @Html.Label("Új hozzávaló", htmlAttributes: new { @class = "control-label col-md-2" })
        <div>
            <input id="Button1" type="button"
                   value="+" onclick="DynamicDiv();" />
            <div id="foo"></div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Feltöltés" />
            </div>
        </div>
    </div>
</div>
        }

    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/jqueryui")

        <script type="text/javascript">
            $(function () {
                var diff = ["Könnyű", "Közepes", "Nehéz", "Nagyon nehéz"];
                $("#slider").slider({
                    value: 2,
                    min: 1,
                    max: 4,
                    step: 1,
                    slide: function (event, ui) {
                        $("#amount").val(diff[ui.value - 1]);
                    }
                });
                $("#amount").val("Közepes");
            });
        </script>

        <script type="text/javascript">
            $(function () {
                $("#slider-range-min2").slider({
                    range: "min",
                    value: 5,
                    min: 1,
                    max: 100,
                    slide: function (event, ui) {
                        $("#time").val(ui.value + " fő");
                    }
                });
                $("#time").val($("#slider-range-min2").slider("value") + " fő");
            });
        </script>

        <script type="text/javascript">
            $(function () {
                $("#slider-range-min").slider({
                    range: "min",
                    value: 30,
                    min: 15,
                    max: 100,
                    slide: function (event, ui) {
                        $("#portion").val(ui.value + " perc");
                    }
                });
                $("#portion").val($("#slider-range-min").slider("value") + " perc");
            });
        </script>

        <script type="text/javascript">
            $(function () {
                $("#slider-range-min3").slider({
                    range: "min",
                    value: 500,
                    min: 100,
                    max: 10000,
                    slide: function (event, ui) {
                        $("#price").val(ui.value + " Ft");
                    }
                });
                $("#price").val($("#slider-range-min3").slider("value") + " Ft");
            });
        </script>

        <script type="text/javascript">
            $("<div>ASD</div>").appendTo("div#main");
        </script>

        <script type="text/javascript" language="javascript">
            function DynamicDiv() {
                var dynDiv = document.createElement("div");
                dynDiv.id = "divDyna";
                dynDiv.innerHTML = "Név:</label><div class=\"col-md-10\"><input class=\"form-control text-box single-line\" id=\"Preparation\" name=\"Preparation\" type=\"text\" value=\"\" /><span class=\"field-validation-valid text-danger\" data-valmsg-for=\"Preparation\" data-valmsg-replace=\"true\"></span></div></div>"
                //dynDiv.innerHTML = "<div class=\"form-group\"><label class=\"control-label col-md-2\" for=\"Elk_sz_t_s\">Név</label><div class=\"col-md-10\"><input class=\"form-control text-box single-line\" id=\"Preparation\" name=\"Preparation\" type=\"text\" value=\"\" /><span class=\"field-validation-valid text-danger\" data-valmsg-for=\"Preparation\" data-valmsg-replace=\"true\"></span></div></div>"
                //dynDiv.innerHTML = "<label>Név: <input type='text'>";
                dynDiv.className = 'form-group control-label col-md-2';
                document.getElementById("foo").appendChild(dynDiv);
            }
        </script>
    }

编辑:完整视图:

kubectl rolling-update

0 个答案:

没有答案