Dispay data from database to textbox asp.net mvc - html

How will I properly display data from database to textbox when the button "search" is clicked? It should display the item name and the price of that product code
enter image description here
Controller
public ActionResult Index(string searchString)
{
using(MyDatabaseEntities db = new MyDatabaseEntities())
{
var products = from s in db.Products
select s;
if (!String.IsNullOrEmpty(searchString))
{
products = products.Where(s => s.ProductCode.Equals(searchString));
}
return View(products.ToList());
}
}
Index
<div class="form-group">
<table style="text-align:left; margin-bottom:10px;">
<tr>
<td>Item Code</td>
<td>#Html.TextBox("SearchString")
</td>
<tr style="background-color:lightgray;">
<td colspan="2"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" class="btn btn-block btn-info"id="buttonsearch" value="Search" />
</td>
</tr>
<tr>
<td> </td>
</tr>
#foreach (var item in Model) {
<tr>
<td style="text-align:left;">Item Name</td
<td>#Html.DisplayFor(modelItem=> item.ProductName)</td>
</tr>
<tr>
<td>Unit Price</td>
<td style="text-align:right;">#Html.DisplayFor(modelItem => item.Price)</td>
</tr>
}
<tr>
<td style="text-align:left;">Quantity</td>
<td>
<input type="text" id="quantity" class="input-sm" />
<span class="error">Product Name required</span>
</td>
</tr>
<tr>
<td style="text-align:left;">Discount</td>
<td>
<input type="text" id="discount" class="input-sm" style="text-align:right;" placeholder="0.00" />
<span class="error">Product Name required</span>
</td>
</tr>
</table>
</div>

Instead of .ToList() use .FirstOrDefault(); just in case you want to populate one record in your textbox.

Related

How do I align button in a table according to the previous button

How do I shift a button to align with the button on the right as follows:
The html as follows:
<table>
<tr>
<td>
<h4> Search: </h4>
</td>
<td>
<input type="text" />
</td>
<td>
<button type="submit" name="abc" form="submitForm" value="Button">Button</button>
</td>
</tr>
<tr>
<td class="customView">
<button type="submit" name="def" form="submitForm" value="Button">Button</button>
</td>
</tr>
</table>
<table>
<tr>
<td>
<h4> Search: </h4>
</td>
<td>
<input type="text" />
</td>
<td>
<button type="submit" name="abc" form="submitForm" value="Button">Button</button>
</td>
</tr>
<tr>
<td colspan="2"></td>
<td class="customView">
<button type="submit" name="def" form="submitForm" value="Button">Button</button>
</td>
</tr>
</table>
add colspan="3" , align="right" to your <td> tag it's work fine,I'm added the snippet below.
<table>
<tr>
<td><h4> Search: </h4></td>
<td><input type="text"/> </td>
<td><button type="submit" name="abc" form="submitForm" value="Button">Button</button></td>
</tr>
<tr >
<td class="customView" colspan="3" align="right"><button type="submit" name="def" form="submitForm" value="Button">Button</button></td>
</tr>
</table>

how to populate values entered in textfield to table

I am trying to generate bill for the customer.When I add items in the form and click on add button I want it to create table and show the bill. But its not working fine. I tried to use sql query but it is showing only 2 items and sometimes blank. when there are 3 4 items inserted into database. Please any one can clear me where am going wrong.
<tr>
<td height="517"><form name="form1" method="post" action="">
<table width="400" border="0" align="center" cellpadding="10" cellspacing="0">
<tr>
<td colspan="2"><div align="center">Add item Details </div></td>
</tr>
<tr>
<td width="117">Item Name</td>
<td width="243"><input name="itemName" type="text" id="itemName">
</td>
</tr>
<tr>
<td>Quantity</td>
<td><input name="qty" type="text" id="qty"></td>
</tr>
<tr>
<td>Price </td>
<td><input name="price" type="text" id="price"></td>
</tr>
<tr>
<td>Vat </td>
<td><input name="vat" type="text" id="vat"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Add"></td>
</tr>
</table>
</form>
<p align="center">Bill Record</p>
<p align="center"> </p>
<table width="70%" border="0" align="center" cellpadding="10" cellspacing="0">
<tr>
<th>Item Name</th>
<th>Quantity</th>
<th>Price</div></th>
<th>Vat</th>
<th>total</th>
</tr>
<% double grandTotal=0;
try
{
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from bill where cname='"+session.getAttribute("cname")+"' order by datetime asc");
while(rs.next())
{
%>
<tr>
<td><%=rs.getString("itemName")%></td>
<td><%=rs.getString("qty")%></td>
<td><%=rs.getString("price")%></td>
<td><%=rs.getString("vat")%></td>
<td><%= rs.getString("total") %></td>
<% grandTotal+=Double.parseDouble( rs.getString("total"));%>
</tr>
<%
} %>
<tr>
<td></td>
<td></td>
<td></td>
<td>grandTotal</td>
<td><%= grandTotal %> </td>
</tr>
<% con.close();
}
catch(Exception e)
{
out.println("Error: "+e);
}
%>
</table>
</tr>
And here is the code where I am adding values into database.
<%
if(request.getMethod().equals("POST"))
{
String itemName="";
int qty;
double price,vat,total;
grandTotal=0;
boolean exists=false;
itemName=request.getParameter("itemName");
qty=Integer.parseInt(request.getParameter("qty"));
price=Double.parseDouble(request.getParameter("price"));
vat=Double.parseDouble(request.getParameter("vat"));
total=price+vat;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
try
{
Statement st=con.createStatement();
st.executeUpdate("INSERT into bill values('"+session.getAttribute("cname")+"','" + dateFormat.format(date) + "','"+itemName+"','"+qty+"','"+price+"','"+vat+"','"+total+"','"+grandTotal+"')");
out.println("<script> alert('Item Added Successfully')</script>");
}

How to control the width of a table with-in table

Here is the page source
<div class="col-xs-12">
<br />
<div class="panel panel-primary">
<div class="panel-heading">
Collection Info
</div>
<div class="panel-body">
<div class="tab-content">
<div class="tab-pane active">
<table class="table" >
<tr>
<th>Vendor</th>
<th>Claim Amount</th>
<th>Amount Received</th>
<th>Type</th>
</tr>
<tr>
<th>Date</th>
<th>Rep</th>
<th>Notes</th>
</tr>
<tr>
<td>
Difference in State Check
</td>
<td>
0.00
</td>
<td>
-382.19
</td>
<td>
<select class="form-control" data-val="true" data-val-required="The Value field is required." id="item_Type_Value" name="item.Type.Value">
<option selected="selected" value="c6edd5bc-c5ef-4a56-8db2-17905d98c823">--None--</option>
<option value="0b4a44d0-7241-422d-935b-d0606b9c5ce1">Attn</option>
<option value="a1341e3c-afb7-4c47-8040-d33bdf82493b">$$$</option>
</select>
<p />
<div class="col-xs-12" style="text-align: right;">
<div class="form-group">
<label class="col-xs-5 control-label"></label>
<div class="col-xs-7">
<input type="submit" value=" Update " style="height:35px;width:75px;" />
</div>
</div>
</div>
</td>
<td>
0.00
</td>
<td>
</td>
<td>
999-999-9999
</td>
</tr>
<tr>
<td>
<textarea cols="80" htmlAttributes="{ class = form-control }" id="Notes" name="Notes" rows="6">
</textarea>
</td>
</tr>
</table>
I'm using bootstrap and below is the picture of my page.
My question is: How can I have Vendor, Claim Amount, Amount Received, Type with-in the div of Collection Info?
so I'm rendering this page using two loops
the first loop generates - vendor, claim amount, amount received, type
the second loop generates - date,rep,notes
and the reason my page is messed up is due to my textarea
here is my html page looks like:
<div class="col-xs-12">
<br />
<div class="panel panel-primary">
<div class="panel-heading">
Collection Info
</div>
<br />
<div class="panel-body">
<div class="tab-content">
<div class="tab-pane active">
<table class="table" >
<tr>
<th>Vendor</th>
<th>Claim Amount</th>
<th>Amount Received</th>
<th>Type</th>
</tr>
<tr>
<th>Date</th>
<th>Rep</th>
<th>Notes</th>
</tr>
#foreach (var item in Model)
{
<tr><td>#item.vendor</td></tr>
<tr>#item.claimamount</td></tr>
<tr>#item.amountreceived</td></tr>
<tr>#item.type</td></tr>
foreach (var item1 in item.DetailViewModel)
{
<tr>
<td>
#item1.Date
</td>
<td>
#item1.Rep
</td>
<td>
#item1.Notes
</td>
</tr>
}
<tr>
<td>
#Html.TextArea("Notes", "", 6, 80)
</td>
</tr>
}
</table>
</div>
</div>
</div>
</div>
</div>
I'm trying to achieve something like this:
[This solution takes into consideration the generated code from the edit]
As I told you in the comment, just by fixing the different colspan in the table, it already looks a lot better (and closer to what you show in your picture): http://jsfiddle.net/wnqLpsv9/
For example, if the rows have 7 columns, make all the rows have 7 columns (or occupy the space of 7 columns using colspan), or the result will depend on how the browser decides to display the table.
Still, you need to look at the source, because this is a mess of unnecessary <tr> and closing </td> without opening <td>, that won't generate valid code:
#foreach (var item in Model)
{
<tr><td>#item.vendor</td></tr>
<tr>#item.claimamount</td></tr>
<tr>#item.amountreceived</td></tr>
<tr>#item.type</td></tr>
foreach (var item1 in item.DetailViewModel)
{
<tr>
<td>
#item1.Date
</td>
<td>
#item1.Rep
</td>
<td>
#item1.Notes
</td>
</tr>
}
<tr>
<td>
#Html.TextArea("Notes", "", 6, 80)
</td>
</tr>
}
From what you show in the picture, it would need to be something more in this line (and notice that I don't claim this is correct, but more of a guideline on how to fix it):
#foreach (var item in Model)
{
<tr>
<td>#item.vendor</td>
<td>#item.claimamount</td>
<td>#item.amountreceived</td>
<td>#item.type</td>
</tr>
foreach (var item1 in item.DetailViewModel)
{
<tr>
<td>
#item1.Date
</td>
<td>
#item1.Rep
</td>
<td colspan="2">
#item1.Notes
</td>
</tr>
}
<tr>
<td colspan="2"></td>
<td colspan="2">
#Html.TextArea("Notes", "", 6, 80)
</td>
</tr>
}

table form is flickring in my page when dropdown changing in screen?

I designed the table like the below.
<table>
<tr>
<td colspan="1">Name</td>
<td colspan="2">
<span id="filterByApplication"><%= Html.DropDownList("filterByName", new SelectList(Model.Applications, "Id", "Name", Model.SelectedApplication.Name), "ALL", new { #class ="dropmenu" })%></span>
</td>
<td colspan="1">Role</td>
<td colspan="2" style="text-align: left">
<span id="filterByRole"><%= Html.DropDownList("filterByApplicationRole", new SelectList(Model.Roles, "Id", "Name", Model.SelectedRole.Name), "ALL", new { #class = "dropmenu" })%></span>
</td>
</tr>
<tr>
<td colspan="1">ID</td>
<td colspan="2">
<input id="id" type="text" value="" class="input" maxlength="8"/>
</td>
<td colspan="1">UserName</td>
<td colspan="2">
<input id="userName" type="text" value="" placeholder="UserName"/>
</td>
</tr>
<tr>
<td colspan="6">
<input id="btnsearch" type="button" value="Search" onclick="searchrequest()"/>
</td>
</tr>
</table>
Whenever click on name dropdown roles are loading in another dropdown at that time total screen i.t table controls all are aligning dynamically(flickering).please tell me how to avoid this ?
Remove all colspan attributes from your table, except last one. Change it to colspan="4" (instead of 6).
If you need to set column's width, you can use CSS styles. For example:
<tr>
<td style="width: 10%">
Id
</td>
<td style="width: 40%">
...
</td>
<td style="width: 10%">
Username
</td>
<td style="width: 40%">
...
</td>
</tr>

Cannot align checkbox in table to the left

I have a checkbox in a table and I cannot align them to the left.
<tr>
<td colspan="3" class="cb">
<input type="checkbox" name="cb" value="checked" /> this is a checkbox
</td>
</tr>
table.all td.cb{
color: #424242;
border:1px solid black;
margin:0 auto;
text-align:left;
}
I have tried many things, but the result is this:
I applied a border for demonstration purposes.
If I add float:left, I get this:
I used the same code in another webpage where it was working fine (there the table had 2 columns only).
Solution:
I already had this in my .css file which strangely caused the problem in spite of the colspan="3"
table.all td input{
width:90%;
float:left;
}
I changed it to
table.all td input.i{
width:90%;
float:left;
}
and added a class="i" to all the other input types and now it's working fine.
The whole form if necessary:
<div id="container_center">
<form>
<table class="minden">
<tr>
<td colspan="3">
<p class="title">Create new account</p>
</td>
</tr>
<tr>
<td colspan="3">
<p class="title2">Enter account holder information</p>
</td>
</tr>
<tr>
<td colspan="3">
<div id="container_jobb_content_vonal"> </div>
</td>
</tr>
<tr>
<td>
<p>First name: *</p>
</td>
<td colspan="2">
<p>Last name: *</p>
</td>
</tr>
<tr>
<td>
<input type="text" name="firstname" size="45">
</td>
<td colspan="2">
<input type="text" name="lastname" size="45">
</td>
</tr>
<tr>
<td>
<p>Email: *</p>
</td>
<td colspan="2">
<p>Email again: *</p>
</td>
</tr>
<tr>
<td>
<input type="text" name="email" size="45">
</td>
<td colspan="2">
<input type="text" name="email2" size="45">
</td>
</tr>
<tr>
<td>
<p>Address 1: *</p>
</td>
<td colspan="2">
<p>Address 2:</p>
</td>
</tr>
<tr>
<td>
<input type="text" name="address1" size="45">
</td>
<td colspan="2">
<input type="text" name="address2" size="45">
</td>
</tr>
<tr>
<td>
<p>Country: *</p>
</td>
</tr>
<tr>
<td>
<select name="country" class="sel_country">
<option value="">[Please Select]</option>
<option value="United States">United States</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Germany">Germany</option>
</select>
</td>
</tr>
<tr>
<td>
<p>City: *</p>
</td>
<td>
<p>State/Province: *</p>
</td>
<td>
<p>Zip code: *</p>
</td>
</tr>
<tr>
<td>
<input type="text" name="city" size="45">
</td>
<td>
<input type="text" name="state" size="30">
</td>
<td>
<input type="text" name="zip" size="10" class="zip">
</td>
</tr>
<tr>
<td>
<p>Phone number: *</p>
</td>
</tr>
<tr>
<td>
<input type="text" name="phone" size="45">
</td>
</tr>
<tr>
<td colspan="3">
<p class="title3">Create your login</p>
</td>
</tr>
<tr>
<td colspan="3">
<div id="container_jobb_content_vonal"> </div>
</td>
</tr>
<tr>
<td>
<p>Username: *</p>
</td>
</tr>
<tr>
<td>
<input type="text" name="username" size="45">
</td>
</tr>
<tr>
<td>
<p>Password: *</p>
</td>
<td>
<p>Confirm password: *</p>
</td>
</tr>
<tr>
<td>
<input type="text" name="password" size="45">
</td>
<td colspan="2">
<input type="text" name="password2" size="45">
</td>
</tr>
<tr>
<td colspan="3">
<p class="title3">Accept terms</p>
</td>
</tr>
<tr>
<td colspan="3">
<div id="container_jobb_content_vonal"> </div>
</td>
</tr>
<tr>
<td colspan="3" class="cb">
<input type="checkbox" name="cb" value="checked"/>this is a checkbox
</td>
</tr>
<tr>
<td>
<ul>
<li>Terms of service</li>
<li>Privacy policy</li>
</ul>
</td>
</tr>
<tr>
<td class="submit" colspan="3">
<input type="submit" name="purchase" value="Purchase" id="submitbutton">
</td>
</tr>
</table>
</form>
</div>
You could also accomplish this by excluding the checkbox from your input style. For example, I was encountering a similar problem with this CSS:
input {
width: 300;
}
And fixed by changing to this:
input:not([type="checkbox"]) {
width: 300;
}