I have table [comments](ID, TITLE, SUMMARY, ID_COMMENT_FATHER)
ID_COMMENT_FATHER: the comment replay for comment father.(Defalut is 0);
Normally, I will write a function using recursive:
writeContentComment(idCommentFather){//the first is 0
DataTable table = getCommentOfCommentFather(idCommentFather);
for(int i = 0;i<table.Rows.Count;i++){
<div class="comment_item">
<div class="comment_item_id">
table.Rows[i]["ID"]
</div>
<div class="comment_item_id">
table.Rows[i]["TITLE"]
</div>
writeContentComment(table.Rows[i]["ID"]);
</div>
}
}
but in MVC, i want select all comments into a DataTable in Controller, then in View, i using this DataTable to write content. I had select table [comments] into [temp_comments](ID,TITLE,SUMMARY,ID_COMMENT_FATHER,LEVEL) using WITH Clause in SQL but i don't
know How to write content of DataTable temp_comments into View Can you help me? Thankyou very much!
basically you want to load a tree of comments form a datatable....something like this should work:
var allNodes= (from x in context.MyTable select x).ToList();
The above statement loads all nodes from the DB. Then, allNodes.Where(x => x.Parent == null) returns all top level tree nodes. Once you have done this all nodes will have their parent and childre propertiels already filled.
@model IEnumerable<YourApp.Models.DataTable> // this goes at top of page
// insert this where ever you want your data to display
foreach (var item in Model)
{
<div class="comment_item">
@item.ID
</div>
<div class="comment_item_id">
@item.TITLE
</div>
}
tinhphong007
Member
3 Points
27 Posts
Recursive! Help!
May 30, 2012 04:11 AM|LINK
Hi all!
I have table [comments](ID, TITLE, SUMMARY, ID_COMMENT_FATHER)
ID_COMMENT_FATHER: the comment replay for comment father.(Defalut is 0);
Normally, I will write a function using recursive:
writeContentComment(idCommentFather){//the first is 0 DataTable table = getCommentOfCommentFather(idCommentFather); for(int i = 0;i<table.Rows.Count;i++){ <div class="comment_item"> <div class="comment_item_id"> table.Rows[i]["ID"] </div> <div class="comment_item_id"> table.Rows[i]["TITLE"] </div> writeContentComment(table.Rows[i]["ID"]); </div> } }but in MVC, i want select all comments into a DataTable in Controller, then in View, i using this DataTable to write content. I had select table [comments] into [temp_comments](ID,TITLE,SUMMARY,ID_COMMENT_FATHER,LEVEL) using WITH Clause in SQL but i don't know How to write content of DataTable temp_comments into View Can you help me? Thankyou very much!
francesco ab...
All-Star
20912 Points
3279 Posts
Re: Recursive! Help!
May 30, 2012 06:16 AM|LINK
basically you want to load a tree of comments form a datatable....something like this should work:
var allNodes= (from x in context.MyTable select x).ToList();
The above statement loads all nodes from the DB. Then, allNodes.Where(x => x.Parent == null) returns all top level tree nodes. Once you have done this all nodes will have their parent and childre propertiels already filled.
Mvc Controls Toolkit | Data Moving Plug-in Videos
tinhphong007
Member
3 Points
27 Posts
Re: Recursive! Help!
May 30, 2012 08:11 AM|LINK
Thank for replay!
I write a function:
public string GenerateComment(int fatherID, int productID) { DataTable tableComment = GetCommentWithFatherId(fatherID, productID); if (tableComment.Rows.Count > 0) { int t = 0; for (int i = 0; i < tableComment.Rows.Count; i++) { t++; tempComment += "<div class='comment_item'>"; tempComment += "<div class='comment_item_user'>"; tempComment += "<strong>" + tableComment.Rows[i]["NAMES"] + "</strong>-" + tableComment.Rows[i]["EMAIL"]; tempComment += "</div>"; tempComment += "<div class='comment_item_action'>"; tempComment += "<div class='comment_item_action_item'>"; tempComment += "<a href='#' class='display_form_replay' id='display-form-replay-" + tableComment.Rows[i]["ID"] + "'>Trả lời</a>"; tempComment += "</div>"; tempComment += "</div>"; tempComment += "<div class='comment_item_title'>"; tempComment += "<strong>" + tableComment.Rows[i]["TITLE"] + "</strong> (" + tableComment.Rows[i]["DATE_TIME"] + ")"; tempComment += "</div>"; tempComment += "<div class='comment_item_content'>"; tempComment += tableComment.Rows[i]["CONTENTS"].ToString(); tempComment += "</div>"; tempComment += "<div class='comment_item_form_replay' id='comment-item-form-replay-" + tableComment.Rows[i]["ID"] + "'>"; tempComment += "<form action='#' method='post'>"; tempComment += "<input type='text' name='title_comment' class='txt_comment_item_replay' placeholder='Tiêu đề' />"; tempComment += "<input type='text' name='name_comment' class='txt_comment_item_replay' placeholder='Họ tên' />"; tempComment += "<input type='text' name='email_comment' class='txt_comment_item_replay' placeholder='Email' />"; tempComment += "<textarea rows='' cols='' name='content_comment' class='txtarea_comment_item_replay' placeholder='Ý kiến của bạn' ></textarea>"; tempComment += "<input type='button' name='bt_comment' class='bt_comment_item_replay' value='' />"; tempComment += "</form>"; tempComment += "</div>"; if (t < 5) { GenerateComment(Convert.ToInt16(tableComment.Rows[i]["ID"]), productID); } } tempComment += "</div>"; } return tempComment; }And function GetCommentWithFatherId:
public DataTable GetCommentWithFatherId(int fatherID, int productID) { DataAccess objDataAccess = new DataAccess(); DbCommand command = objDataAccess.CreateCommand(); command.CommandText = "PROC_GET_COMMENT_OF_PRODUCT_WITH_FATHERID"; DbParameter param; param = command.CreateParameter(); param.ParameterName = "@FATHERID"; param.Value = fatherID; param.DbType = DbType.Int16; command.Parameters.Add(param); param = command.CreateParameter(); param.ParameterName = "@PRODUCTID"; param.Value = productID; param.DbType = DbType.Int16; command.Parameters.Add(param); DataTable table = objDataAccess.ExecuteSelectCommand(command); return table; }But code html generate is a mess(see very bad). also code html in code C#(!good).
I know in Razor View, we can write function, but like in Controller, I want:
@functions{ public void GenerateComment(int fatherID, int productID){ DataTable tableComment = GetCommentWithFatherId(fatherID, productID); if (tableComment.Rows.Count > 0) { int t = 0; for (int i = 0; i < tableComment.Rows.Count; i++) { t++; <div class='comment_item'> <div class='comment_item_user'> <strong> tableComment.Rows[i]["NAMES"] </strong>- tableComment.Rows[i]["EMAIL"] </div> if (t < 5) { GenerateComment((int)tableComment.Rows[i]["ID"], productID); } } </div> } } }Hihi! How could it be that not?
francesco ab...
All-Star
20912 Points
3279 Posts
Re: Recursive! Help!
May 31, 2012 07:42 AM|LINK
You cannot write this in the controller. However you can use a StringBuilder object to build your Html:
StringBuilder sb=new StringBuilder();
sb.Append("<div........");
........
At end you do
sb.ToString()
to get the complete string.
Mvc Controls Toolkit | Data Moving Plug-in Videos
JohnLocke
Contributor
3216 Points
710 Posts
Re: Recursive! Help!
May 31, 2012 12:40 PM|LINK
In your controller, you can handle the query and send that data to your View where you can then format it using div containers.
Controller:
View:
@model IEnumerable<YourApp.Models.DataTable> // this goes at top of page // insert this where ever you want your data to display foreach (var item in Model) { <div class="comment_item"> @item.ID </div> <div class="comment_item_id"> @item.TITLE </div> }tinhphong007
Member
3 Points
27 Posts
Re: Recursive! Help!
May 31, 2012 03:51 PM|LINK
Thank everyone!