![](/img/trans.png)
[英]How can I make my controller catch the data submitted by a form from one of his views in ASP.NET MVC
[英]how a user comments form can be submitted and displayed on same page in asp.net mvc 5
我正在一個項目中,用戶可以上傳評論,並且我想在用戶上傳數據的同一頁面上顯示所有上傳的數據,我該如何實現這一點,請幫我解決這個問題的好方法是什么。嘗試了以下事情,但我無法獲得預期的結果
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; }
}
您可以返回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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.