Can I sent values from collection in model through submit? - c#

I have this table
<table id="Products" class="Products">
<tr>
<th>ProductId</th>
<th>Productname</th>
<th>Quantity</th>
<th>UnitPrice</th>
</tr>
<% for(int i=0; i < Model.NorthOrderDetails.Count; i++)
{
%>
<tr>
<td><%: Html.Label(Model.NorthOrderDetails[i].ProductID.ToString()) %></td>
<td><%: Html.Label(Model.NorthOrderDetails[i].ProductName) %> </td>
<td><%: Html.TextBoxFor(m => m.NorthOrderDetails[i].Quantity) %></td>
<td><%: Html.TextBoxFor(m => m.NorthOrderDetails[i].UnitPrice) %></td>
<td> <input type="submit" value="?" Name="qodel" /> </td>
</tr>
<% } %>
</table>
<% } %>
And I want to sent to controller the value of ProductID in line which I press button. How I can do it?

So without Javascript you are limiting yourself slightly here, I think the code below should work but I'm sure someone can poke a hole in it!
<table id="Products" class="Products">
<thead>
<tr>
<th>ProductId</th>
<th>Productname</th>
<th>Quantity</th>
<th>UnitPrice</th>
</tr>
</thead>
<tbody>
#{
for(int i=0; i < Model.NorthOrderDetails.Count; i++)
{
<tr>
<td>
#Html.Label(Model.NorthOrderDetails[i].ProductID.ToString())
</td>
<td>
#Html.Label(Model.NorthOrderDetails[i].ProductName)
</td>
<td>
#Html.TextBoxFor(m => m.NorthOrderDetails[i].Quantity)
</td>
<td>
#Html.TextBoxFor(m => m.NorthOrderDetails[i].UnitPrice)
</td>
<td>
#Html.ActionLink("Edit", "Vi")
</td>
<td>
<input type="submit" name="submitValue"
value="#Model.NorthOrderDetails[i].SomeValue"/>
</td>
</tr>
}
}
</tbody>
</table>
Then make sure that your controller action accepts the name attribute of the input as a parameter:
public ActionResult YourControllerMethod(string submitValue) {
....
}
The code would look something like that in MVC3+, <% is from MVC2.

Related

How to show tr with number td in view mvc?

I have data as list, I want to break each row with 2 columns. I tried so hard but it is not working. This is sample html. I have 4 data rows, it should show two rows, each row with two columns.
<table>
<tr>
<td>
<table>
<tr>
<td>
<table>
<tr>
<td>TYPE</td>
<td>Blue</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table>
<tr class='bg-red'>
<td>13:45</td>
<td>2017ABC-0001</td>
</tr>
<tr>
<td>12:45</td>
<td>2017WEX-0002</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>
<table>
<tr>
<td>TYPE</td>
<td>Red</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table>
<tr class='bg-red'>
<td>13:45</td>
<td>2017ABC-0001</td>
</tr>
<tr>
<td>12:45</td>
<td>2017WEX-0002</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td>
<table>
<tr>
<td>TYPE</td>
<td>Green</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table>
<tr class='bg-red'>
<td>13:45</td>
<td>2017ABC-0001</td>
</tr>
<tr>
<td>12:45</td>
<td>2017WEX-0002</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>
<table>
<tr>
<td>TYPE</td>
<td>Yellow</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table>
<tr class='bg-red'>
<td>13:45</td>
<td>2017ABC-0001</td>
</tr>
<tr>
<td>12:45</td>
<td>2017WEX-0002</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
And this is sample code
string result = "<table>";
List<string> listTYPE = GetTYPE();
foreach (var item in listTYPE)
{
result += "<tr><td><table> <tr><td><table><tr><td>TYPE</td><td>"
+ item + "</td></tr></table></td></tr><tr><td><table>";
List<Detail_Product> listDetail = GetDetail(item);
foreach (var detail in listDetail)
{
if (detail.PO_NO.Contains("ABC"))
{
result += "<tr class='class='bg-red''><td>" + detail.CREATE_DATE + "</td><td>" + detail.PO_NO + "</td>";
}
result += "<tr><td>" + detail.CREATE_DATE + " </td><td>" + detail.PO_NO + "</td>";
}
result += "</tr></table></td></tr> </table></td></tr>";
}
result += "</table>";
ViewBag.result = result;
What should I do?, I thought to check if (listLINE.IndexOf(line) > 2) I will break new <tr> but I don't know how to do it.

Column 'Fecha' does not belong to table

The data type of 'fecha' on sql server is datetime. How can I show this data type datetime to this table below.
<tbody>
<% for (var data = 0; data < TableData.Rows.Count; data++)
{ %>
<tr>
<td>
Contraloría General de la República Dominicana
</td>
<td>
<%=TableData.Rows[data]["NOMBRE"]%>
</td>
<td>
<%=TableData.Rows[data]["PUESTO"]%>
</td>
<td>
<%=TableData.Rows[data]["ESTADO"]%>
</td>
<td>
<%=TableData.Rows[data]["Fecha"]%>
</td>
<td class="auto-style1">
<%if (TableData.Rows[data][3].ToString() == "True")
{ %>
<input type="checkbox" class="chcktbl1" checked="checked" name="chcktbl1" data-id="<%=TableData.Rows[data]["ID"]%>" />
<%} %>
<%else
{ %>
<input type="checkbox" class="chcktbl1" name="chcktbl1" id="checkbox" data-id="<%=TableData.Rows[data]["ID"]%>" />
<%} %>
</td>
</tr>
<% } %>
</tbody>
How to add the data type datetime in the table?
Be sure for case in your array index (accessing sql by the index is case sensitive)
<td>
<%=TableData.Rows[data]["FECHA"]%>
</td>
You could try this:
<td>
<%=TableData.Rows[data]["FECHA"].ToString("dd-MMM-yyyy")%>
</td>
Regards.

Parsing a HTML structure

Below is my html structure (table):
<table>
<tr><td>A</td></tr>
</table>
<table>
<tr><td>B</td></tr>
</table>
<table>
<table>
<table>
<table>
<tbody>
<tr class="A">
<td>
ABC
</td>
<td>
Link
</td>
</tr>
<tr class="B">
<td>
DEF
</td>
<td>
Link2
</td>
</tr>
</tbody>
</table>
</table>
</table>
</table>
I tried to get data as below:
HtmlNode thediv = doc.DocumentNode.SelectSingleNode("//table[3]//table[1]");
⇒ It works well.
But, I tried with code as below to get data ABC/DEF in table 3.
HtmlNode thediv = doc.DocumentNode.SelectSingleNode(
"//table[3]//table[1]//table[2]//table[3]");
⇒ Not OK.
I think what you actually want is
var bothNodes = doc.DocumentNode.SelectNodes("//table[3]//table[1]//tr/td[1]/text()");
That will give you both nodes ABC and DEF of the third table
You can try it here: XPathFiddle
Your code doesn't work because there is no node that fits the second query.
Step by Step:
This is your original html:
<table>
<tr><td>A</td></tr>
</table>
<table>
<tr><td>B</td></tr>
</table>
<table>
<table>
<table>
<table>
<tbody>
<tr class="A">
<td>
ABC
</td>
<td>
Link
</td>
</tr>
<tr class="B">
<td>
DEF
</td>
<td>
Link2
</td>
</tr>
</tbody>
</table>
</table>
</table>
</table>
//table[3] gives you the third table
<table>
<table>
<table>
<table>
<tbody>
<tr class="A">
<td>
ABC
</td>
<td>
Link
</td>
</tr>
<tr class="B">
<td>
DEF
</td>
<td>
Link2
</td>
</tr>
</tbody>
</table>
</table>
</table>
</table>
//table[3]//table[1] gives you the first table that's a descendant of the third table.
<table>
<table>
<table>
<tbody>
<tr class="A">
<td>
ABC
</td>
<td>
Link
</td>
</tr>
<tr class="B">
<td>
DEF
</td>
<td>
Link2
</td>
</tr>
</tbody>
</table>
</table>
</table>
//table[3]//table[1]//table[2] would give you the second table that's a descendant the first table that's a descendant of the third table. And there is only one --> doesn't work.
Hi Manfred Radlwimmer,
Thank for your answer. I did it :).
The code is below:
if (doc.DocumentNode.SelectNodes("//table") != null)
{
HtmlNode thediv = doc.DocumentNode.SelectSingleNode("//table[3]//table[1]//tr/td[1]//tr[3]//table//tr/td[2]//table");
HtmlNodeCollection cells = thediv.SelectNodes("tr");
for (var j = 1; j < cells.Count; ++j)
{
var data= cells[j].InnerText;
}
}

How I can get data from table in MVC

I have aspx page. I dislay data in table. How I can get new values from this table in Controller. Or how I can generate grid/table another method in aspx page in mvc?
<table id="Products" class="Products">
<tr>
<th>ProductId</th>
<th>Productname</th>
<th>Quantity</th>
<th>UnitPrice</th>
</tr>
<% foreach (var item in Model.NorthOrderDetails)
{
%>
<tr>
<td><%: item.ProductID %></td>
<td><%: item.ProductName %></td>
<td><%: Html.TextBox("Quantity",item.Quantity) %></td>
<td><%: Html.TextBox("UnitPrice",item.UnitPrice) %></td>
<td> <%: Html.ActionLink("Update", "View2") %></td>
</tr>
<% } %>
</table>
The table should be wrapped in a form. Each text box should have a unique 'id' attribute. This can be done with a few simple changes to your code.
Something like this:
<form>
<table id="Products" class="Products">
<tr>
<th>ProductId</th>
<th>Productname</th>
<th>Quantity</th>
<th>UnitPrice</th>
</tr>
<% foreach (var item in Model.NorthOrderDetails)
{
%>
<tr>
<td><%: item.ProductID %></td>
<td><%: item.ProductName %></td>
<td><%: Html.TextBoxFor(m => item.Quantity) %></td>
<td><%: Html.TextBoxFor(m => item.UnitPrice) %></td>
</tr>
<% } %>
</table>
<button type="submit">Update</button>
</form>
Let us know if this helps!

Assigning "HTML Select Control" Values in PageLoad() Event

I want to Use only the HTML Controls on my page. For that i want to use some text boxes and a HTML select(List box) and a Submit button for Post Event.
The Code is as follows:
<script type="text/javascript">
function dropdown(mySel) {
var myWin, myVal;
myVal = mySel.options[mySel.selectedIndex].value;
if (myVal) {
if (mySel.form.target) myWin = parent[mySel.form.target];
else myWin = window;
if (!myWin) return true;
myWin.location = myVal;
}
return false;
}
</script>
This Java Script i am using for the HTML Select List.
The Rest of the Code are As follows:
<form action="feedback.aspx" method="post" onsubmit="return dropdown(this.feeds)">
<h1>
Let us know your inconvenience for the following Listing:
</h1>
<table>
<tr>
<td>
<b>Type : </b>
</td>
<td>
<asp:Label ID="lbltype" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<b>ID : </b>
</td>
<td>
<asp:Label ID="lblID" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<b>Title: </b>
</td>
<td>
<asp:Label ID="lblTitle" runat="server"></asp:Label>
</td>
</tr>
</table>
<table>
<tr>
<td>
User ID:
</td>
<td>
<input type="text" name="txtUserid" />
</td>
</tr>
<tr>
<td>
Name:
</td>
<td>
<input type="text" name="txtName" />
</td>
</tr>
<tr>
<td>
Contact:
</td>
<td>
<input type="text" name="txtContact" />
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<input type="text" name="txtemail" />
</td>
</tr>
</table>
<br />
<table cellpadding="5" cellspacing="5" width="500px">
<tr>
<td style="width: 400px">
<b>Please Select the given option </b>
</td>
</tr>
<tr>
<td style="width: 400px">
<select name="feeds">
<option value="">Choose an Option...</option>
<option value="">Option1</option>
<option value="">Option2</option>
<option value="">Option3</option>
</select>
</td>
</tr>
<tr>
<td style="width: 400px">
<b>Write your Comment Here: </b>
<br />
<textarea id="TextArea" name="txtcomment" cols="45" rows="5"></textarea>
</td>
<td>
</td>
</tr>
<Efextra:Captcha ID="Captcha1" runat="server" TabIndex="29" ValidationErrorMessage="Verification Code"
ValidationGroup="Feeds" ValidationText="*"></Efextra:Captcha>
<tr>
<td>
<input type="submit" value="Submit FeedBack" validationgroup="Feeds" />
</td>
</tr>
</table>
</form>
I have a XML document having the following Format:
<?xml version="1.0" encoding="utf-8" ?>
<Feedback>
<Options>
<ID>0</ID>
<option>[Select your option]</option>
</Options>
<Options>
<ID>1</ID>
<option>I got Incomplete/wrong information posted here.</option>
</Options>
<Options>
<ID>2</ID>
<option>The Content is not in Proper Format.</option>
</Options>
<Options>
<ID>3</ID>
<option>How can get the similar type of properties?</option>
</Options>
<Options>
<ID>4</ID>
<option>This Listing is Sold and Still Displaying here.</option>
</Options>
<Options>
<ID>5</ID>
<option>I can not send SMS for this Listing.</option>
</Options>
<Options>
<ID>6</ID>
<option>I can not get the Contact Information of the owner of this post.</option>
</Options>
<Options>
<ID>7</ID>
<option>My problem is not given here!</option>
</Options>
</Feedback>
Now what i ahve to do is:
When Page load it checks whether the user is registered or not, if he/She is a registered user, the first four field [User ID, Name, Contact, Email] can be be loaded itself.
for this my code behind file assigns value like this:
User FeedUser = Propertywala.User.GetAuthUser();
Txt1.Text=Feeduser.UserID; and so on..
If he/she is not a registered user then User will get this field blank and i will get fetch all the filled values through Page.Request().
Second Issue is that i want to load the Option of select box from XML file which is above.
I am a newbie to this code and flow. so please anybody can help me doing this.
Thanks in Advance.
You'll need to assign the runat="server" attribute to the input type="text" controls as well. Then you can access them in codebehind just like a webcontrol. The same goes for your select html control.
For the Xml document you're going to need an XmlReader object. For the implementation of that, please look up MSDN
Finally i did it without using a single asp control. I solved the problem like this:
<form name="feedback" action="FeedBack.aspx">
<h1>
Let us know your inconvenience for the following Listing:
</h1>
<table>
<%if (IsUserLoggedIn) %>
<%{ %>
<tr>
<td>
<b>User ID:</b>
</td>
<td>
<input type="text" value="<%=VisitingUser.UserIdn%>" size="30" readonly="readonly" />
</td>
</tr>
<tr>
<td>
<b>Name:</b>
</td>
<td>
<input type="text" value="<%=VisitingUser.FullName%>" size="30" readonly="readonly" />
</td>
</tr>
<tr>
<td>
<b>Contact:</b>
</td>
<td>
<input type="text" value="<%=VisitingUser.MobileNumber %>" size="30" readonly="readonly" />
</td>
</tr>
<tr>
<td>
<b>Email:</b>
</td>
<td>
<input type="text" value="<%=VisitingUser.PrimaryEmail%>" size="30" readonly="readonly" />
</td>
</tr>
<%} %>
<%else%>
<%{ %><tr>
<td>
<h3>
Enter the Folowing details to submit the Feedback:
</h3>
</td>
</tr>
<tr>
<td>
Name:
</td>
<td>
<input type="text" name="txtName" />
</td>
</tr>
<tr>
<td>
Contact:
</td>
<td>
<input type="text" name="txtContact" />
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<input type="text" name="txtEmail" />
</td>
</tr>
<%} %>
</table>
<br />
<%if (int.TryParse(Request.QueryString["property"], out Property)) %>
<%{ %>
<%Propertywala.Property prp = new Propertywala.Property(Property);%>
<%if (prp.OwnerId > 0) %>
<%{ %>
<table>
<tr>
<td>
<b>Property ID :</b>
</td>
<td>
<%=Property.ToString() %>
</td>
</tr>
<tr>
<td>
<b>Property Title :</b>
</td>
<td>
<%=prp.AutoGeneratedTitle %>
</td>
</tr>
</table>
<%} %>
<%} %>
<%else if (int.TryParse(Request.QueryString["project"], out Project)) %>
<%{ %>
<%Propertywala.Project prj = new Propertywala.Project(Project);%>
<%if (prj.UserId > 0) %>
<%{ %><table>
<tr>
<td>
<b>Project ID : </b>
</td>
<td>
<%=Project.ToString() %>
</td>
</tr>
<tr>
<td>
<b>Project Title : </b>
</td>
<td>
<%=prj.Title %>
</td>
</tr>
</table>
<%} %>
<%} %>
<table cellpadding="5" cellspacing="5" width="500px">
<tr>
<td style="width: 400px">
<b>Please Select the given option </b>
</td>
</tr>
<tr>
<td style="width: 400px">
<select name="feeds">
<%foreach (System.Xml.XmlNode xNode in FeedXmlList)%>
<%{ %>
<option value="<%=xNode.ChildNodes[1].InnerText%>">
<%=xNode.ChildNodes[1].InnerText%></option>
<%} %>
</select>
</td>
</tr>
<tr>
<td style="width: 400px">
<b>Write your Comment Here: </b>
<br />
<textarea name="txtcomment" cols="45" rows="5"></textarea>
</td>
<td>
</td>
</tr>
<Efextra:Captcha ID="Captcha1" runat="server" TabIndex="29" ValidationErrorMessage="Verification Code"
ValidationGroup="Feeds" ValidationText="*"></Efextra:Captcha>
<tr>
<td>
<input type="submit" value="Submit FeedBack" />
</td>
</tr>
</table>
</form>
And finally in Code behind file i just put some required condition and fetch all the html values through request.form, like this:
if (Request.HttpMethod.ToLower() == "post" && Request.Form.Count > 0)
{
if (!string.IsNullOrEmpty(Request.Form["feeds"]) || !string.IsNullOrEmpty(Request.Form["txtcomment"]))
{
if (!string.IsNullOrEmpty(Request["property"]) || !string.IsNullOrEmpty(Request["project"]))
{
if (IsUserLoggedIn||(!IsUserLoggedIn && !string.IsNullOrEmpty(Request["txtName"]) && !string.IsNullOrEmpty(Request["txtContact"]) && !string.IsNullOrEmpty(Request["txtEmail"])))
{
if (Request.Form["feeds"] != "[Select your option]")
{
Mail(Request.Form["feeds"], Request.Form["txtcomment"]);
}
else
{
Common.ClientMessage("Select the Option and Retry to Submit the feeds.", CommonConsts.AlertType.info);
}
}
else
{
Common.ClientMessage("Unregistered user must fill their name and Contact Details!", CommonConsts.AlertType.info);
}
}
}
}

Categories