I have a list which I created in the controller:
var PayList = new[] {
new ListEntry { Id = 1, Name = "" },
new ListEntry { Id = 2, Name = "Yes" },
new ListEntry { Id = 3, Name = "No" }
};
ViewBag.PayList = new SelectList(PayList, "Id", "Name");
In the view I have the following:
#Html.DropDownList("Pay", new SelectList(ViewBag.PayList,"Id","Name"))
When I try to display it, it says the following:
DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'Id'. Not sure why this is not working.
Also how do I default a value to the select list. I like to default it to "Yes". I thought there was a way to do do this from the controller.
Your ViewBag.PayList is already the type of SelectList. I don't see a reason to create the SelectList twice, so shouldn't it just be:
#Html.DropDownList("Pay", ViewBag.PayList)
or
#Html.DropDownList("Pay", ViewBag.PayList as SelectList)
(I don't ever use the ViewBag, so I'm not sure if your version is strongly typed).
Just use
#Html.DropDownList("Pay", ViewBag.PayList)
In your view
Try this Way:
<div id="divmsg" style="color: green; font-weight: bold">
#ViewBag.Msg
</div>
<div id="divmsg2" style="color: red; font-weight: bold">#ViewBag.Msg2</div>
<div id="quality" style="width: 80%;" align="center">
<input type="hidden" value="#ViewBag.ProjectId" id="ProjectId_" class="projectId"/>
<input type="hidden" value="#ViewBag.ProjectName" id="ProjectName_" class="projectName"/>
<input type="hidden" value="#ViewBag.UserId" class="UserId_" id="UserId"/>
<input type="hidden" value="#ViewBag.TempId" class="TempId_" id="TempId" />
<div class="toggle-contents">
<table width="100%" id="qualitygoal">
<tr>
<td class="even" align="left">
#Html.Label("Project Id")
</td>
<td class="even" align="left">
#ViewBag.ProjectId
</td>
</tr>
<tr>
<td class="projectname" align="left">
#Html.Label("Project Name")
</td>
<td class="projectname" align="left">
#ViewBag.ProjectName
</td>
</tr>
</table>
<table width="100%" id="qualitygoal1" class="tbl">
<tbody>
<tr>
<th align="center">DestinationColumns</th>
<th align="center">SourceColumns</th>
</tr>
#foreach (var data in Model)
{
<tr>
<td>
<span class="spanStatus" id="lblStatus_#data.TempId" destinationID = "#data.Destination">#data.Destinationvalue</span>
<select class="status" id="ddlStatus_#data.TempId">
<option value="0">--Select--</option>
<option value="4">TICKET ID</option>
<option value="5">DESCRIPTION</option>
<option value="6">TICKET CATEGERY</option>
<option value="7">SEVIORITY/PRIORITY</option>
<option value="8">STATUS</option>
<option value="9">CREATED DATE</option>
<option value="10">CREATED BY</option>
<option value="11">ASSIGNED TO</option>
<option value="12">ASSIGNED DATE</option>
<option value="13">REPSONSE ETA</option>
<option value="14">RESOLUTION ETA</option>
<option value="15">RESPONSE DATE</option>
<option value="16">RESOLUTION DATE</option>
<option value="17">ROOT CAUSE/MODULE</option>
<option value="18">REOPEN FLAG (Y/N)</option>
<option value="19">CLOSE DATE</option>
<option value="20">SLA MET (Y/N)</option>
</select>
</td>
<td>
<span class="spanSource" id="lblSource_#data.TempId" >#data.Source</span>
<input class="Source" id="txtSource_#data.TempId" type="text" value="#data.Source" maxlength="30" />
</td>
<td>
<table style="width: 50%;">
<tr>
<td>
<input class="edit" id="Edit_#data.TempId" type="button" value="Edit" />
<input class="update" id="Update_#data.TempId" type="button" value="Update" />
</td>
<td class="Gcancle" id="Canc_#data.TempId">
<input class="gridcancel" id="Cancel_#data.TempId" type="button" value="Cancel" />
</td>
</tr>
</table>
</td>
<td>
<table style="width: 50%;">
<tr>
<td>
<input class="delete" id="Delete_#data.TempId" type="button" value="Delete" />
</td>
</tr>
</table>
</td>
</tr>
}
</tbody>
</table>
</div>
<div align="right">
<input type="button" value="Add New Row" class="Add" />
<input type="button" value="Save" class="saved" />
<input type="button" value="Close" class="cancel" />
</div>
</div>
Related
I have a modal, within that modal there are various card. Those cards have dropdown. It looks some what like the screenshot below. They all have different select name. Is there any way to write a method in C# so I dont have to do //select[#name='test1a']or //select[#name='test1b'] for all dropdown?
HTML Code below
<table class="row-inner-table" border="0" cellpadding="1" cellspacing="1">
<tbody>
<tr class="row-content">
<td class="field-name">test1a</td>
<td class="field-value">
<select name="test-4" onchange="calc(document.myForm.TOTAL_8,this,24)">
<option value="" score="0"> </option>
<option value="118" score="0">test1b </option>
<option value="124" score="1">test1c</option>
<option value="120" score="2">test1d</option>
</select>
<input type="HIDDEN" name="PREVIOUS_24" value="0">
</td>
</tr>
<tr class="row-content">
<td class="field-name">test2</td>
<td class="field-value">
<select name="test-5" onchange="calc(document.myForm.TOTAL_8,this,25)">
<option value="" score="0"> </option>
<option value="121" score="0">hi</option>
<option value="113" score="1">hello</option>
<option value="114" score="2">C ya </option>
</select>
<input type="HIDDEN" name="PREVIOUS_25" value="0">
</td>
</tr>
<tr class="row-content">
<td class="field-name">test 3</td>
<td class="field-value">
<select name="test-6" onchange="calc(document.myForm.TOTAL_8,this,26)">
<option value="" score="0"> </option>
<option value="123" score="0">hehe</option>
<option value="112" score="1">haha</option>
<option value="119" score="2">001</option>
</select>
<input type="HIDDEN" name="PREVIOUS_26" value="0">
</td>
</tr>
<tr class="row-content">
<td class="field-name">test4</td>
<td class="field-value">
<select name="test-7" onchange="calc(document.myForm.TOTAL_8,this,27)">
<option value="" score="0"> </option>
<option value="111" score="0">404</option>
<option value="122" score="1">402/option>
<option value="117" score="2">403</option>
</select>
<input type="HIDDEN" name="PREVIOUS_27" value="0">
</td>
</tr>
<tr class="row-content">
<td class="field-name">test 5 </td>
<td class="field-value">
<select name="test-8" onchange="calc(document.myForm.TOTAL_8,this,28)">
<option value="" score="0"> </option>
<option value="115" score="0">Relaxed</option>
<option value="125" score="1">Tense, rigid</option>
<option value="116" score="2">Very tense or rigid</option>
</select>
<input type="HIDDEN" name="PREVIOUS_28" value="0">
</td>
</tr>
<tr class="row-content-total">
<td class="field-name total-label">Total</td>
<td class="field-value">
<input type="TEXT" size="5" name="TOTAL_8" value="0">
</td>
</tr>
</tbody>
</table>
It should be as easy as something like this
public SelectElement dropdownElement(By locator)
{
return new SelectElement(driver.FindElement(locator));
}
and you call it like
SelectElement select = dropdownElement(By.CssSelector("select[name='test-5']"));
Here is another version of C.Peck's solution which will cut down the repetitive Xpath (or CSS) string code, assuming the base of the Xpath is always the same which in the OP it appears to be
public SelectElement SelectMenuItem(string menuValue)
{
var menuString = $"//select[#name='{menuValue}']";
return new SelectElement(driver.FindElement(By.XPath(menuString)));
}
SelectElement selectOne = SelectMenuItem("test-4");
SelectElement selectTwo = SelectMenuItem("test-5");
EDIT:
To show you the A-Z on reusability for this, it would look something like this...
Method to return Select Element with dynamic XPath string:
public SelectElement SelectMenuItem(string menuValue)
{
var menuString = $"//select[#name='{menuValue}']";
return new SelectElement(_driver.FindElement(By.XPath(menuString)));
}
Method to set value for dropdown, by XPath:
public void SetMenuValue(string menuValue, string menuDropDown)
{
SelectElement select = SelectMenuItem(menuDropDown);
select.SelectByText(menuValue);
}
Finally, method to set the specific values you want, passing in value first, then XPath next:
public void Input_Values()
{
SetMenuValue("118", "test-4");
SetMenuValue("121", "test-5");
}
You don't need that last method, you could call SetMenuValue from anywhere, that was just another example of how you could do this
Hello I have a form with several html/input tags. Like so:
<form class="form-group">
<div class="body table-responsive">
<table class="table m-b-0">
<thead>
<tr>
<th>Name</th>
<th>age</th>
<th>phone number</th>
<th>ID</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>#item.name</td>
<td>
<input type="text" name="" id="" value=""
class="bordered">
</td>
<td>
<input type="text" name="" id="" value=""
class="bordered">
</td>
<td>
<input type="text" name="" id="" value=""
class="bordered">
</td>
</tr>
}
</tbody>
</table>
</div>
</form>
I havent named or id them yet , since I know there is a way to fill all inputs and save them all at the same time when a submit or button is pressed, I just dont know how.
Do I need to add something a tag or attribute on the in order to be able to save all inputs at the same time when the is pressed?
You have to assign names to each input and add place a submit button. When you click submit, user input will be sent to the server with the given method.
<form method="post" class="form-group">
<div class="body table-responsive">
<table class="table m-b-0">
<thead>
<tr>
<th>Name</th>
<th>age</th>
<th>phone number</th>
<th>ID</th>
</tr>
</thead>
<tbody>
#{
var i = 0;
foreach (var item in Model) {
<tr>
<td>#item.name</td>
<td>
<input type="text" name="input1_#i" id="" value=""
class="bordered">
</td>
<td>
<input type="text" name="input2_#i" id="" value=""
class="bordered">
</td>
<td>
<input type="text" name="input3_#i" id="" value=""
class="bordered">
</td>
</tr>
}
}
</tbody>
</table>
</div>
<button type="submit">Submit</button>
</form>
Or you can listen to the submit event and handle it yourself.
$('form').submit(function(e) {
e.preventDefault();
// you can get the input data from 'this' (form element)
// here comes your validation and server call
});
I am trying to located input element "mpcname" in js file which is under div and table elements.
The code I have written to locate the element is as follows but is not detecting the Input element with the name mpcName in it.
Driver.FindElement(By.XPath(".//input[contains(.,'mpcName')]"));
The HTML code looks like below.
<div enable-tooltips="" class="ng-scope">
<form name="InstanceDetailForm" ng-submit="doApply()" novalidate="" class="ng-pristine ng-invalid ng-invalid-required ng-valid-unnamed ng-valid-maxlength ng-valid-not-integer">
<table class="table-condensed" style="width: 100%;">
<tbody><tr>
<td></td>
</tr>
<tr>
<td>
<table style="width: 100%;">
<tbody><tr>
<td class="pull-left">
<h4 class="ng-binding">Controller Configuration<b ng-show="InstanceIsDirty()" class="ng-hide"> *</b></h4>
</td>
<td class="pull-right">
<button id="btnApply" type="submit" class="btn btn-primary" data-toggle="tooltip" data-placement="bottom" title="Save an APC instance">
<span class="glyphicon glyphicon-save"></span> Apply
</button>
<button id="btnExportConfig" type="button" class="btn btn-primary" ng-click="goToInstanceList()" data-toggle="tooltip" data-placement="bottom" title="Cancel save and redirect to APC instance list page.">
<span class="glyphicon glyphicon-remove"></span> Cancel
</button>
</td>
</tr>
</tbody></table>
</td>
</tr>
<tr>
<td>
<table style="width: 100%" class="table-condensed table-striped table-bordered">
<tbody><tr>
<td>Name</td>
<td>
**<input id="mpcname" name="mpcname" ng-model="mpcname" value="" ng-required="true" is-named="" size="50" autocomplete="off" ng-disabled="!allowEditInstanceName(mpcname,state)" class="ng-pristine ng-empty ng-invalid ng-invalid-required ng-valid-unnamed ng-touched" required="required" type="text">**
With xpath ".//input[contains(.,'mpcName')]" you are actually looking for an <input> element with text mpcName. mpcName is actually the id (and name and ng-model) attribute. You can use
Driver.FindElement(By.Id("mpcName"));
Or
Driver.FindElement(By.Name("mpcName"));
following code may be helpful
Driver.FindElement(By.XPath(".//input[contains(#id,'mpc')]");
Driver.FindElement(By.XPath(".//input[contains(#class,'ng-pris')]");
I am receiving text to a function public void LoadMarkup(HtmlTextWriter writer){} and I need to remove last <tr> tag from it.
The text I am getting is:
<table class="form">
<tr>
<td class="createLabel">Name:<span class="field">*</span></td><td><input type="text" maxlength="200" id="productTitle" name="productTitle" class="required" style="width: 250px;" /></td>
</tr><tr>
<td class="createLabel">Account:<span class="field">*</span></td><td><select id="recognizedAccount" name="recognizedAccount" class="required">
<option value="8a1dd4c7-8ddd-4632-aa81-6be29dc86d13">
Donation
</option><option value="5617660d-4794-4cad-aab8-a04323faf68a">
Dues Account
</option>
</select></td>
</tr><tr style="display: none;">
<td class="createLabel">Deferral Method:</td><td><select id="deferralMethod" name="deferralMethod" class="required">
<option value="e816071c-282d-46af-8c5d-ebecb8472b94">
None
</option>
</select></td>
</tr><tr>
<td class="createLabel">Description:<span class="required-field">*</span></td><td><textarea id="productDescription" name="productDescription" class="required" rows="3" style="width: 250px;"></textarea></td>
</tr><tr>
<td class="createLabel">Active:</td><td><input type="checkbox" id="productActive" name="productActive" /></td>
</tr><tr>
<td class="createLabel">Expiration Date:<span class="required-field">*</span></td><td><span class="datePicker"><input type="text" autocomplete="off" id="productExpirationDate" name="productExpirationDate" class="required" style="width: 135px;" /></span></td>
</tr><tr class="typeRates">
<td class="createLabel">Rate:<span class="required-field">*</span></td><td><input id="5a2da3f6-c3e5-4cd0-a194-7a704c8f07e0_rate" name="5a2da3f6-c3e5-4cd0-a194-7a704c8f07e0_rate" class="required" type="text" /></td>
</tr>
I need to remove this text from Upper text:
<tr class="typeRates">
<td class="createLabel">Rate:<span class="required-field">*</span></td><td><input id="5a2da3f6-c3e5-4cd0-a194-7a704c8f07e0_rate" name="5a2da3f6-c3e5-4cd0-a194-7a704c8f07e0_rate" class="required" type="text" /></td>
</tr>
I can not modify text before I am receiving it in function as this is coming from framework. Any one can help with it please suggest.
Thanks.
Not tested, nor the most robust, but perhaps give it a try:
public string RemoveLastTableRow(HtmlTextWriter writer)
{
// Create an array which contains each table row split
string[] html = writer.ToString().Split(new string[] { "<tr>" }, StringSplitOptions.None);
// Return all the array indexes joined, except for the final index
return string.Join("", html.Take(html.Count() - 1));
}
I have the following HTML file, I want to get each H2 (Standard (Flexible Rate).. and Executive (Flexible Rate) ... with the Room Only , Breakfast Included .
Then push the Room Only and Breakfast Include with 2 Prices of each into objects where I have Standard along with 2 prices from Room Only and Breakfast Included, and the same for Executive
I tried Fizzler with AgilityPack however, I couldn't get the correct results, could you please suggest me an idea or one good parser for this case? Thanks
<div id="accordionResizer" style="padding:5px; height:300px; border-radius:6px;" class="ui-widget-content regestancias">
<div id="accordion" class="dias">
<h2>
<a href="#">
Standard (Flexible Rate) from 139 €
</a>
</h2>
<div class="estancias_precios estancias_precios_new">
<table style="width: 285px;">
<tr class="" title="">
<cont>
<td style="width: 25px;">
<input type="radio" name="estancias" id="tarifa602385" elem="tarifa" idelem="602" idreg="385" precio="139" reg="Only%20Bed" nombre="Standard%20%28Flexible%20Rate%29" />
</td>
<td style="width: 155px;">
<label class="descrip" for="tarifa602385" precio="139.00" reg="Only%20Bed" nombre="Standard%20%28Flexible%20Rate%29">
Only Bed
</label>
</td>
<td style="width: 55px;"></td>
<td style="width: 55px;">
<strong class="precios_mos">139.00 €</strong>
</td>
</cont>
</tr>
<tr class="" title="">
<cont>
<td style="width: 25px;">
<input type="radio" name="estancias" id="tarifa602386" elem="tarifa" idelem="602" idreg="386" precio="156.9" reg="Breakfast%20Included" nombre="Standard%20%28Flexible%20Rate%29" />
</td>
<td style="width: 155px;">
<label class="descrip" for="tarifa602386" precio="156.90" reg="Breakfast%20Included" nombre="Standard%20%28Flexible%20Rate%29">
Breakfast Included
</label>
</td>
<td style="width: 55px;"></td>
<td style="width: 55px;">
<strong class="precios_mos">156.90 €</strong>
</td>
</cont>
</tr>
</table>
</div>
<h2>
<a href="#">
Executive (Flexible Rate) from 169 €
</a>
</h2>
<div class="estancias_precios estancias_precios_new">
<table style="width: 285px;">
<tr class="" title="">
<cont>
<td style="width: 25px;">
<input type="radio" name="estancias" id="tarifa666385" elem="tarifa" idelem="666" idreg="385" precio="169" reg="Only%20Bed" nombre="Executive%20%28Flexible%20Rate%29" />
</td>
<td style="width: 155px;">
<label class="descrip" for="tarifa666385" precio="169.00" reg="Only%20Bed" nombre="Executive%20%28Flexible%20Rate%29">
Only Bed
</label>
</td>
<td style="width: 55px;"></td>
<td style="width: 55px;">
<strong class="precios_mos">169.00 €</strong>
</td>
</cont>
</tr>
<tr class="" title="">
<cont>
<td style="width: 25px;">
<input type="radio" name="estancias" id="tarifa666386" elem="tarifa" idelem="666" idreg="386" precio="186.9" reg="Breakfast%20Included" nombre="Executive%20%28Flexible%20Rate%29" />
</td>
<td style="width: 155px;">
<label class="descrip" for="tarifa666386" precio="186.90" reg="Breakfast%20Included" nombre="Executive%20%28Flexible%20Rate%29">
Breakfast Included
</label>
</td>
<td style="width: 55px;"></td>
<td style="width: 55px;">
<strong class="precios_mos">186.90 €</strong>
</td>
</cont>
</tr>
</table>
</div>
</div>
</div>
Here you go with a quick and dirty approach:
class RoomInfo
{
public String Name { get; set; }
public Dictionary<String, Double> Prices { get; set; }
}
private static void HtmlFile()
{
List<RoomInfo> rooms = new List<RoomInfo>();
HtmlDocument document = new HtmlDocument();
document.Load("file.txt");
var h2Nodes = document.DocumentNode.SelectNodes("//h2");
foreach (var h2Node in h2Nodes)
{
RoomInfo roomInfo = new RoomInfo
{
Name = h2Node.InnerText.Trim(),
Prices = new Dictionary<string, double>()
};
var labels = h2Node.NextSibling.NextSibling.SelectNodes(".//label");
foreach (var label in labels)
{
roomInfo.Prices.Add(label.InnerText.Trim(), Convert.ToDouble(label.Attributes["precio"].Value, CultureInfo.InvariantCulture));
}
rooms.Add(roomInfo);
}
}
The rest is up to you! ;-)