ویو مدل:
کد HTML:
public class Comment_ViewModel { public Comment_ViewModel() { Id = Guid.NewGuid(); InsertDateTime = DateTime.Now; }
/// <summary> /// کد يکتايي نظر /// </summary> //**************************** [Display(Name = "کد يکتايي", Order = 10000)] //**************************** public Guid Id { get; set; }
/// <summary> /// کليد والد پيام /// </summary> //***************************************** [Display(Name = "کد يکتايي والد", Order = 10000)] //[Editable(false)] //***************************************** public Guid? ParentId { get; set; }
/// <summary> /// آي دي پست يا کالايي که نظر براي آن ثبت خواهد شد /// </summary> //***************************************** [Required] [Editable(false)] [Display(Name = "کد يکتايي کالا", Order = 10000)] //***************************************** public Guid ProductId { get; set; }
/// <summary> /// نام کاربر نظر دهنده /// </summary> //***************************************** [Required] [StringLength(maximumLength: 20)] [Editable(false)] [Display(Name = "نام", Order = 10000)] //***************************************** public string Name { get; set; }
/// <summary> /// ايميل کاربر نظر دهنده /// </summary> //***************************************** [Required] [Editable(false)] [DataType(dataType: DataType.EmailAddress)] [Display(Name = "ايميل", Order = 10000)] //***************************************** public string Email { get; set; }
/// <summary> /// نظر کاربر /// </summary> //***************************************** [Required] [StringLength(maximumLength: 100)] [DataType(dataType: DataType.MultilineText)] [Display(Name = "نظر", Order = 10000)] //***************************************** public string CommentBox { get; set; }
/// <summary> /// در حال نمايش بودن يا نبودن نظر /// </summary> //***************************************** [Display(Name = "فعال؟", Order = 10000)] //***************************************** public bool CommentIsActive { get; set; }
/// <summary> /// تاريخ و ساعت ثبت نظر /// </summary> //***************************************** [ScaffoldColumn(false)] [Display(Name = "تاريخ و ساعت ثبت نظر", Order = 10000)] [Editable(false)] //***************************************** public DateTime InsertDateTime { get; set; }
}
جاییکه قراره پارامترها ارسال شود:
کد HTML:
<div class="panel panel-default" style="margin-top:30px;"> <div class="panel-heading"> <h2>ليست نظرات فعال</h2> </div> <table class="table table-striped" style="overflow:auto"> <thead> <tr> <th class="label-in-table"> @Html.DisplayNameFor(model => model.Name) </th> <th class="label-in-table"> @Html.DisplayNameFor(model => model.Email) </th> <th class="label-in-table"> @Html.DisplayNameFor(model => model.CommentBox) </th> @*<th class="label-in-table"> @Html.DisplayNameFor(model => model.CommentIsActive) </th>*@ <th class="label-in-table"> @Html.DisplayNameFor(model => model.InsertDateTime) </th> <th></th> </tr> </thead> <tbody> @if (Model.Where(m=>m.CommentIsActive==true).Count() == 0) { <tr> <td colspan="6" class="text-center labels"> هيچکدام از نظرات فعال نيستند</td> </tr> } else { foreach (var item in Model) { if (item.CommentIsActive == true) { <tr> <td class="label-in-table"> @Html.DisplayFor(modelItem => item.Name) </td> <td class="label-in-table"> @Html.DisplayFor(modelItem => item.Email) </td> <td class="label-in-table"> @Html.DisplayFor(modelItem => item.CommentBox) </td> @*<td class="label-in-table"> @Html.DisplayFor(modelItem => item.CommentIsActive) </td>*@ <td class="label-in-table"> @Html.DisplayFor(modelItem => item.InsertDateTime) </td> <td> @Html.ActionLink("ويرايش", "Edit", new { id = item.Id }, new { @class = "anchors" }) | @Html.ActionLink("جزييات", "Details", new { id = item.Id }, new { @class = "anchors" }) | @Html.ActionLink("حذف", "Delete", new { id = item.Id }, new { @class = "anchors" }) | @{ ViewBag.Id = Guid.NewGuid(); ViewBag.Parameters = "parentId=" + item.Id + "&&id=" + ViewBag.Id; } @Html.ActionLink("پاسخ", "Create", "Comments", new { @ViewBag.Parameters }, new { @class = "anchors"}) @*چطور بيشتر از يک پارامتر را کوري استرينگ ارسال کنم؟ , id =_id, parentId = item.Id *@ </td> </tr> }
} } </tbody> </table>
</div>
<script type="text/javascript"> $(document).ready(function () { $("[id*=btnReply]").each(function () { $(this).click(function () { var commentid = $(this).attr("id").match(/[\d]+$/); $("#ParentId").val(commentid); }); }); });</script>
روت کانفیگ:
کد HTML:
routes.MapRoute( name: "Create", url: "{controller}/{action}/{parentId}/{id}", defaults: new { controller = "Comments", action = "Create", parentId= UrlParameter.Optional ,id= 000000000-0000-0000-0000-000000000000 } );
کد اکشن مورد نظر:
public ActionResult Create(Guid? parentId,Guid id)
{
var viewModel = new Comment_ViewModel();
viewModel.ParentId = parentId;
viewModel.ProductId = id;
viewModel.Email = UserManager.GetEmail(User.Identity.GetUserId());
viewModel.Name= User.Identity.GetUserName();
//***
//ViewBag.ProductId = id;
//ViewBag.ParentId = parentId;
return View(viewModel);
}