ASP.NET MVC displaying user Data such as comments? - asp.net

hows the best way to display comments/user data from a db using mvc?
do we just do our own for loop and manually display it or? will doing it this way cause paging problems in the future? any best methods or practice?
jquery?
thanks guys.

When I first started learning MVC, I put a lot of conditional code in the view to manage the display of the data and controls. However, once I figured out the benefit of HTML helpers, I found that it not only reduced the code in my views, but it also made it easier to test the resulting output.
As an example, this is a code fragment for an entry form that would appear in the finished HTML page:
<table class="listing">
<tr>
<td class="prompt">Last Name:</td>
<td><input id="LastName" name="LastName" value="Smith" /></td>
</tr>
<tr>
<td class="prompt">First Name:</td>
<td><input id="FirstName" name="FirstName" value="John" /></td>
</tr>
<tr>
<td class="prompt">Job Title:</td>
<td><input id="JobTitle" name="JobTitle" value="Manager" /></td>
</tr>
</table>
Building the view using standard MVC helpers would result in code that looks something like this:
<table class="listing">
<tr>
<td class="prompt">Last Name:</td>
<td><%= Html.TextBox("LastName", Model.LastName) %></td>
</tr>
<tr>
<td class="prompt">First Name:</td>
<td><%= Html.TextBox("FirstName", Model.FirstName) %></td>
</tr>
<tr>
<td class="prompt">Job Title:</td>
<td><%= Html.TextBox("JobTitle", Model.JobTitle) %></td>
</tr>
</table>
However, I was able to create HTML helpers that allowed me to make the view look like this:
<table class="listing">
<%= Html.Edit_LastName(Model.LastName) %>
<%= Html.Edit_FirstName(Model.FirstName) %>
<%= Html.Edit_JobTitle(Model.JobTitle) %>
</table>
Here are the HTML helpers:
private string Edit_Field(string prompt, string fieldName, string value)
{
string textBox = helper.TextBox(fieldName, value);
return string.Format("<tr><td class='prompt'>{0}</td><td>{1}</td></tr>", prompt, textBox);
}
public string Edit_LastName(this HtmlHelper helper, string value)
{
return EditField("Last Name:", "LastName", value);
}
public string Edit_FirstName(this HtmlHelper helper, string value)
{
return EditField("First Name:", "FirstName", value);
}
public string Edit_JobTitle(this HtmlHelper helper, string value)
{
return EditField("Job Title:", "JobTitle", value);
}
This method is not nearly as straightforward as the standard method, but if you have similar controls that are used on multiple views, this approach clearly helps with maintenance. For example, if you want to change the prompt text from "Job Title" to "Position", you just change the prompt string in the Edit_JobTitle method and the change is reflected in every view where that method is called.

is the best way to use client jquery
with tablesorter or server side for
loop?
I don't think there is a connection between these two. Anyways you have to use an iterator to get all the rows from the database. Then you can use jQuery plugin to view the data in pages.
Here is a nice implementation of this
Table Sorting, Paging and Filtering with jQuery

IMO, use the iterator. I use jquery plugins when I need a display format that is complex. jquery itslef is useful when you need to access/modify dom or make ajax calls. Paging shouldn't affect this decision or vice-versa.

Related

Storing user input in classic asp

What i have to create is a form that i migrated out of access. I created the 4 asp pages. Now what they would like is to store the user input values some how until they hit the final page before writing to the SQL database. What is the easiest way to store this data until the final page for submission? I could try an array or maybe even java script?
You could use the session object - http://msdn.microsoft.com/en-us/library/ms525095(v=vs.90).aspx
As suggested in this other answer, you could use session variables. The alternative is to just pass them from page to page using hidden form fields, eg
<input name="yourvariable" type="hidden" value="<%=Request.Form("yourvariable")%>
There's no correct answer, it's a case of which you're most comfortable with
"get_user_info.asp"
<html>
<head></head>
<body>
<form id='frm_user_info' name='frm_user_info' method='post' action='get_user_info_go.asp'>
<table cellspacing='3'>
<tr>
<td align=right>Name</td>
<td><input type='text' id='s_name' name='s_name' size='40'></td>
</tr>
<tr>
<td></td>
<td><input type='submit' value='Next'></td>
</tr>
</table>
</form>
</body>
</html>
"get_user_info_go.asp"
<%
s_name=request.form("s_name")
if (s_name="") then response.redirect("get_user_info.asp")
session("s_name")=s_name
response.redirect("next_page.asp")
%>

Unable to have fixed form field postions while dynamically adding new fileds in a form

I have a simple password reset dialog, which enables users to set a custom password.
<form id="resetPassword"
action="XXXXX">
<fieldset><input type="hidden" name="userId" value="${user.id}" />
<table>
<tbody>
<tr>
<td><input id="resetCustomPassword" class="ui-widget" type="checkbox"></td>
<td><label>Set Custom Password</label></td>
</tr>
</tbody>
<tbody style="display: none;">
<tr>
<td><label for="newPassword"> New Password: </label></td>
<td><input id="newPassword" class="ui-widget" type="password"></td>
</tr>
<tr>
<td><label for="reTypeNewPassword"> Retype New Password: </label></td>
<td><input id="reTypeNewPassword" class="ui-widget" type="password"></td>
</tr>
</tbody>
Now, if a user checks for "Set Custom password", I dynamically show the new password fields. The problem is in my this label in the above form:
<td><label>Set Custom Password</label></td>
This gets dynamically adjusted to the RIGHT side, so it aligns with the new password fields.
How can I let my above label be static, and not adjust itself dynamically?
Thanks!
Try this:
<td align="left"><label>Set Custom Password</label></td>
You're far better off using styled unordered lists as containers for forms, rather than tables.
See: http://www.alistapart.com/articles/multicolumnlists

how to return different view from different method in controller in MVC

can anyone help me?
senario is..
I have UserController(UC) and Browse View in MVC, In browse page I have three buttons and thats why i have written javascript to call perticular method in UC, now within UC I am sending data to Model (which is comming from View) and I am able to search from model also. ok..then after searched data is coming from model I am sending that data to view following code.
objUser = user.Search(1, "Ramesh", "", "", "", "", FMEnums.UserStatus.InActive);
if (objUser.Count <= 0)
{
ViewData["Message"] = "Unauthorised";
return View("Browse", objUser);
}
else
{
return View("Browse", objUser);
}
and in View( that is Browse.aspx) by giving
( <%# Page Title="Max 2.0" Language="C#" MasterPageFile="~/Views/Shared/Master.Master" Inherits="System.Web.Mvc.ViewPage>"%> )
at the start of my Browse page I am accepting that data. ok...
and rendering that incoming data by following code..
<%if(Model != null){ %>
<tr>
<td class="td_bgcolor"> User Name </td>
<td class="td_bgcolor"> First Name </td>
<td class="td_bgcolor"> Last Name </td>
<td class="td_bgcolor"> e-mail </td>
<td class="td_bgcolor"> Role </td>
<td class="td_bgcolor"> Staus </td>
<td class="td_bgcolor"> </td>
</tr>
<% foreach( var user in Model)%>
<%{ %>
<tr>
<td><%=user.UserName %></td>
<td><%=user.FirstName %></td>
<td><%=user.LastName %></td>
<td><%=user.Email %></td>
<td><%=user.Status %></td>
<td><%=user.Roles %></td>
<td>
Delete
</td>
</tr>
<%} %>
<%} %>
Butt..
now the problem is...it is successfully debugging but my Browse page is not comming with data instead browser is rendering "User/Search" page which is not even in the project.
plzzzzzzz can any one have solution?
It's hard to say without knowing what you have in your routes. Especially if your getting a page that's not in your project.

Creating a textbox in ASP.Net with unique ID programatically

I'm wondering what the best way to create text boxes in my loop here so that the IDs are unique instead of just "TextBox" for all of them. I need to add up the Price based on the Price per unit and the Quantity.
My items are loaded from a database so there could be any number of them, so I need to generate the fields on the fly. I was thinking maybe an array of fields could be used like in a form application, or is this not the right way to go?
I tried googling an answer, but maybe I'm just not wording my question well. It seems like this should be solved rather easily, but I just can't find the answer.
Here is my code:
<table class="proposal" width="800">
<tr>
<th>Item</th>
<th>Price per Unit</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<% int x = 0; %>
<% do
{
%>
<tr>
<td valign="top">
<%= this.name[x] %><br />
<%= this.desc[x] %></td>
<td valign="top" align="right"><%= "$" + this.price[x] %></td>
<td valign="top" align="center"><asp:TextBox ID="TextBox" runat="server" Width="75px"></asp:TextBox></td>
<td valign="top" align="right"></td>
</tr>
<% x++;
} while (x != this.y);
%>
</table>
You can't do a dynamic id for an asp.net TextBox. Could you use plain html <input type="text"> text boxes instead? Then you could assign whatever id you want.
Another approach would be to use a repeater control to generate your table. Each TextBox will then automatically get a unique id. If you need to get the ids in code, then in the repeater's ItemDataBound method, use FindControl to get a handle to the TextBox and read the ids/
You'll want to use the Repeater control as in this demonstration.

Possible to dynamically add an asp:CheckBox control to a TableCell?

In my .NET application I need to add a checkbox to each row in a dynamically created asp:Table.
Is it possible to do that by dynamically creating an asp:CheckBox for each row and somehow put it inside a TableCell object? (In that case how?)
Or do I need to replace the asp:table control with something else, like a Repeater control or GridView to make it work?
I'm looking for the quickest solution because I don't have much time.
Thanks in advance!
/Ylva
in aspx:
<asp:Table id=T1 runat=server />
in cs:
TableCell tc;
foreach(TableRow tr in T1.Rows)
{
tr.Cells.Add(tc = new TableCell());
((IParserAccessor)tc).AddParsedSubObject(new CheckBox());
}
You do not want to do it on server side( in the cs as Yossarian said). because every time your page is reloaded or refreshed, you would have to recreate those checkboxes, which would mean new checkboxes every load, which also would mean your checkbox controls info will be lost because they are not on the client side, so all updated info done by the user (checkbox checked)will be lost, so you want be able to find out what is checked unless you add jquery in and it starts to get more complicated then it needs to be
if you are using webpages then it would be best to use asp:Gridview web control and bind the data to the table in code behind as so:
Gridview.Datasource=//ex:data;
Gridview.Databind();
As shown in the example on this page here
but if you are using MVC then you would add them in the client code in a form as so:
<% using (Html.BeginForm("Presentation", "Home")) %>
<% { %>
<table id="Table" class="color" width="100%" border="1">
<colgroup width="3%" ></colgroup>
<colgroup width="15%"></colgroup>
<colgroup width="20%"></colgroup>
<colgroup width="15%"></colgroup>
<colgroup width="47%"></colgroup>
<thead>
<tr class="dxgvHeader_Glass">
<th id="CheckBox" class="style1" ><input type="checkbox" class="selectall" id="selectall" name="CheckBox" /></th>
<th id="DateTime" runat="server"></th>
<th id="Description" runat="server"></th>
</tr>
</thead>
<tbody >
<%try
{ %>
<% foreach (var SamAuditLog in ViewData.Model)
{ %>
<tr>
<td class="style1" align="center"><%=Html.CheckBox(""+data.ID) %></td>
<td><%= data.DateTime%></td>
<td><%= data.Description%></td>
</tr>
<% } %>
<%} %>
</tbody>

Resources