如何在ASP.NET MVC 5中提交用户评论表单并将其显示在同一页面上

时间:2018-10-27 05:07:43

标签: c# sql-server asp.net-mvc linq

我正在一个项目中,用户可以上传评论,并且我想在用户上传数据的同一页面上显示所有上传的数据,我如何实现这一目标,请帮我解决这个问题的好方法是什么.i尝试了以下操作,但无法获得预期的结果

            public ActionResult Gandhiji(int? id)
            {
                ViewBag.Message = "150 Years Of Gandhiji";

                IEnumerable<UserComments> obj = db.UserComments;
                if(id!=null)
                {
                    obj = obj.Where(p => p.Id == id);
                }

                return View("Gandhiji",obj);
            }




                @model BigFoot.UsercommentsImage
                @using (Html.BeginForm("Gandhiji", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
                {
                    @Html.AntiForgeryToken()
                    <h4 style="margin-left:30px;">User Comments</h4>
                    <div class="form-horizontal">
                        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                        <div class="form-group">
                            @Html.LabelFor(model => model.Name, "Name", htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                            </div>
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.PhoneNumber, "Phone Number", htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
                                @Html.ValidationMessageFor(model => model.PhoneNumber, "", new { @class = "text-danger" })
                            </div>
                        </div>

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

                        <div class="form-group">
                            @Html.LabelFor(model => model.Comments, htmlAttributes: new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.TextAreaFor(model => model.Comments, new { @class = "form-control" })
                                @Html.ValidationMessageFor(model => model.Comments, "", new { @class = "text-danger" })
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="control-label col-md-2">Picture:</label>
                            <div class="col-md-10">
                                <input class="form-control" type="file" id="UploadedFile" name="UploadedFile" />
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-offset-2 col-md-10">
                                <input type="submit" value="Submit" class="btn btn-default" />
                            </div>
                        </div>
                    </div>
                }



    <img src="@Item.Path"/>

            </div>

        </div>



   public class UsercommentsImage
    {
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        [Phone]
        public string PhoneNumber { get; set; }
        [Required]
        [EmailAddress]
        public string Email { get; set; }
        [Required]
        public string Comments { get; set; }
        public string Path { get; set; }
        public HttpPostedFileBase UploadedFile { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

您可以返回json并将其绑定到任何控件,例如输入,标签或跨度。您需要更改return视图以返回json。

JQuery:

$(document).ready(function () {
    $("#InputDate").live('click', function () {
        var date = $("#InputDate").val();
        if (date != "") {
            $.getJSON("/Home/GetNames",
                    { date: $("#InputDate").val() },
                    function (data) {
                        $("#ProviderName").empty();
                        // [...]
                        });
                    });
        }
    });
});

和C#

public JsonResult GetNames(string date)
 {
   List<Provider> list = new List<Provider>();
   // [...]
   return Json(list, JsonRequestBehavior.AllowGet);
}