【问题标题】:how a user comments form can be submitted and displayed on same page in asp.net mvc 5如何在 asp.net mvc 5 的同一页面上提交和显示用户评论表单
【发布时间】:2018-10-27 05:07:42
【问题描述】:

我正在开发一个用户可以上传 cmets 的项目,我想在用户上传数据的同一页面上显示所有上传的数据我如何才能实现这一点,请帮助我解决这个问题的好方法是什么.我尝试了以下事情,但我无法得到预期的结果

            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; }
    }

【问题讨论】:

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


    【解决方案1】:

    您可以返回 json 并将其绑定到任何控件,例如输入、标签或跨度。您需要更改返回视图以返回 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多