How to add a asp-page-handler dynamically to a <a></a> tag in asp.net core? - asp.net-core

bool result=true;
if the result is true then:
<a asp-route-pageIndex="1">1</a>
if the result is false then:
<a>1</a>
how to do it?

#{
//just for illustration
var result = true;
var pageIndex = 1;
}
#if (result)
{
<a asp-route-pageIndex="#pageIndex">#pageIndex</a>
}
else
{
<a>1</a>
}

Related

How to make asp-route-{value} tag helper take value from variable?

I would like to achieve the thing below:
#{
var myParam = "foo";
var myValue = 1;
}
<a asp-controller = "mycontroller" asp-action = "myaction" asp-route-#(myParam) = "#myValue">doit</a>
And it should generate this (according to a default route template):
doit
But this part does not work:
asp-route-#(myParam)
It is possible to do this?
Oh, asp-all-route-data does it.
#{
var myParam = "foo";
var myValue = 1;
var parms = new Dictionary<string, string>
{
{ myParam, myValue.ToString() }
};
}
<a asp-controller="mycontroller" asp-action="myaction" asp-all-route-data="parms">doit</a>

Code upgrade in Vuejs

I have below code in vue.js method.
if(element == 'name'){
var address_data = {
name : this.name
}
}
I would like to replace name using element like below
if(element == 'name'){
var address_data = {
element : this.element
}
}
How can I do it ?
EZ PZ like this:
if (element == 'name') {
var address_data = { element }
}
If element is a variable holding the name of the property in address_data which you want to set, and also the name of the property on this, go like this:
if (element == 'name') {
var address_data = {}
address_data[element] = this[element]
}

Update Product Table with subtable. Razor code and update code of controller is working fine but [HTTPPOST] code is not working, giving errors

#foreach (var sprd in ViewBag.subProduct)
{
<div class="form-row control-group row-fluid">
<div class="controls span2">
<select id="text" style="visibility: hidden;"></select>
</div>
<div class="controls span2">
#sprd.subproduct_pricetitle
#Html.Hidden("title1", (string)#sprd.subproduct_pricetitle)
</div>
#Html.Hidden("subid", (int)#sprd.subproduct_id)
<div class="controls span2">
<div class="input-prepend input-append row-fluid">
<span class="add-on prepend ">£ </span>
#Html.TextBox("combined-input1", (decimal)#sprd.subproduct_price, new { #class = "row-fluid" })
</div>
</div>
</div>
}
This is a code for displaying the data on view page.
public ActionResult Update(int id)
{
IEnumerable<producttype_master> Product_Typee;
int producttypeid = Convert.ToInt32(context.product_master.Find(id).product_type.ToString());
Product_Typee = context.producttype_master.ToList();
ViewBag.productType = new SelectList(Product_Typee, "producttype_id", "producttype_name", producttypeid);
IEnumerable<product_master> products;
products = context.product_master.ToList();
//List<subproduct_master> sprdList = context.subproduct_master.Where(m => m.product_id == id).ToList();
IEnumerable<subproduct_master> sprdList = context.subproduct_master.Where(m => m.product_id == id).ToList();
ViewBag.subProduct = sprdList;
return View(context.product_master.Find(id));
}
This update code is not working well, giving error.
[HttpPost]
public ActionResult Update(product_master prdmaster, subproduct_master subprdmaster, FormCollection formCollection)
{
product_master prd = context.product_master.Where(m => m.product_id == prdmaster.product_id).FirstOrDefault();
if (prd != null)
{
prd.product_type = int.Parse(formCollection["PrdType"]);
prd.product_updateby = 1;
prd.product_updateon = DateTime.Now;
prd.product_isactive = "1";
subproduct_master sprdList = context.subproduct_master.Where(m => m.product_id == prd.product_id).FirstOrDefault();
if (sprdList != null)
{
//string latestId = context.product_master.Max(p => p.product_id).ToString();
for (int i = 1; i <= 7; i++)
{
subproduct_master sprdList2 = context.subproduct_master.Where(m => m.subproduct_id == sprdList.subproduct_id).FirstOrDefault();
sprdList2.subproduct_price = decimal.Parse(formCollection["combined-input" + i]);
sprdList2.subproduct_pricevalidity = Convert.ToDateTime(formCollection["datepicker1"]);
sprdList2.subproduct_updatedon = DateTime.Now;
sprdList2.subproduct_updatedby = 1;
sprdList2.subproduct_isactive = "1";
context.Entry(sprdList2).CurrentValues.SetValues(sprdList);
//context.SaveChanges();
sprdList2.subproduct_id++;
}
}
context.Entry(prd).CurrentValues.SetValues(prdmaster);
context.SaveChanges();
}
return View(prdmaster);
}

Retrieve selected checkboxes in an array into the controller

How can i retrieve the selected checkbox in the controller.
This is the main view where the user can choose a request access.
#using (Html.BeginForm("addBatch_CARF", "CARF", FormMethod.Post, new { #name = "register" }))
{
#Html.ValidationSummary(true)
<div id="formAlert" class="alert alert-danger">
<a class="close">×</a>
<strong>Warning!</strong> Make sure all fields are filled and try again.
</div>
var catName = "";
var displayCan = "";
var candidates = "";
for (int i = 0; i < Model.Count; i++)
{
if (catName != Model[i].request_category)
{
<li class="list-group-item list-group-item-success">
#Html.DisplayFor(modelItem => Model[i].request_category)
<span class="pull-right" style="margin-right:60px;">Special Instructions</span>
</li>
catName = Model[i].request_category;
displayCan = catName;
}
if (displayCan == Model[i].request_category)
{
candidates = Model[i].request_name;
<div class="checkbox_request">
#Html.CheckBoxFor(model => model[i].isSelected, new { #class = "is_selected" })
#Html.DisplayFor(model => model[i].request_name)
#if(Model[i].request_name == "Folder Access")
{
<span class="label label-danger">Pls specify all the drive path. Note: For accessing of drives outside PETC please proceed to Online CARF</span>
}
<span class="pull-right">
#Html.EditorFor(model => model[i].special_instruction)
</span>
#Html.HiddenFor(model => model[i].request_type_id)
#Html.HiddenFor(model => model[i].system_roles_id)
</div>
}
}
<li class="list-group-item list-group-item-success">
Access to:
</li>
<div id="employeeAdd">
#{Html.RenderAction("AddRequestor"); }
</div>
<p class="request_btn">
<button type="submit" class="btn btn-primary" id="addbtn">Save</button>
</p>
}
I have only rendered this view AddRequestor in selecting or adding an employee.
<table class="table table-hover">
#for (int i = 0; i < Model.Count; i++){
<tr>
<th>
#Html.CheckBox("checkbox", new { #class = "is_selected" })
#Html.DisplayFor(model => model[i].FullName)
#Html.HiddenFor(model => model[i].Emp_Badge_No)
</th>
</tr>
}
</table>
The main goal of this is all the chosen employees must have also all the chosen request access.
[HttpPost]
public ActionResult addBatch_CARF(List<Request_Type> list, List<Employees_All_vw> emp, string[] checkboxes)
{
foreach (var x in emp)
{
int num = 1;
bool z = Convert.ToBoolean(num);
if (x.checkbox == z)
{
//add data into CARF table
CARF carf = new CARF();
carf.requestor = x.Emp_Badge_No;
carf.carf_type = "BATCH CARF";
carf.created_by = #User.Identity.Name.Remove(0, 9).ToLower();
carf.created_date = System.DateTime.Now;
carf.active_flag = true;
db.CARves.Add(carf);
db.SaveChanges();
int id = carf.carf_id;
//add data into Request Access Table
foreach (var i in list)
{
int val = 1;
bool y = Convert.ToBoolean(val);
if (i.isSelected == y)
{
Request_Access ra = new Request_Access();
ra.request_access_id = 1;
ra.carf_id = id;
ra.request_type_id = i.request_type_id;
ra.special_instruction = i.special_instruction;
ra.ra_assignee = i.system_roles_id;
ra.dept_approval = null;
ra.dept_approval_date = null;
ra.dept_remarks = null;
ra.final_approval = null;
ra.final_approval_date = null;
ra.final_remarks = null;
ra.acknowledge_by = null;
ra.acknowledge_date = null;
ra.journal = null;
ra.closed_by = null;
ra.closed_date = null;
ra.verified_by = null;
ra.verified_date = null;
db.Request_Access.Add(ra);
}
}
db.SaveChanges();
}
TempData["MessageAlert"] = "Successfully created!";
return RedirectToAction("Batch_CARF");
}
}
I've got an error on this line if (x.checkbox == z)
Operator '==' cannot be applied to operands of type 'string[]' and 'bool'
Your parameter string[] checkboxes contains values that are typeof string (either "True" or "False") so you would need to use the Convert.ToBoolean() method before comparing if (x.checkbox == z). However this will not work since #Html.CheckBox("checkbox", ..) generates 2 inputs type="checkbox" with value="True" and a type="hidden" with value="False" so if its checked, both true and false post back and if its unchecked, then only false is posted. There is no way you can possibly match up which values belong to which employee.
Instead create a view model to represent the selection of employees
public class EmployeeVM
{
public string BadgeNumber { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; }
}
And then in your AddRequestor() method (assumes you have an Employees table)
public ActionResult AddRequestor()
{
List<EmployeeVM> model = db.Employees.Where(e => e.active_flag).Select(e => new EmployeeVM
{
BadgeNumber = e.Emp_Badge_No,
Name = e.FullName
}.ToList();
return PartialView(model);
}
and in the view
#model List<EmployeeVM>
#for (int i = 0; i < Model.Count; i++)
{
#Html.HiddenFor(m => m[i].BadgeNumber)
<label>
#Html.CheckboxFor(m => m[i].IsSelected)
<span>#Html.DisplayFor(m => m[i].Name)</span>
</label>
}
And finally, in the POST method
[HttpPost]
public ActionResult addBatch_CARF(List<Request_Type> list, List<EmployeesVM> employees)
{
// To get the selected employees
IEnumerable<string> selectedEmployees = employees.Where(e => e.IsSelected);
foreach(EmployeesVM employee in selectedEmployees)
{
....
carf.requestor = employee.BadgeNumber;
....

selecting divs fails

I am trying to parse the information inside the div class="base shortstory:
<div id="dle-content">
<div class="base shortstory">
<h3 class="btl">HTC Jetstream</h3>
</div>
<div class="base shortstory">
<h3 class="btl">Samsung S4</h3>
</div>
<div class="base shortstory">
<h3 class="btl">Dell Streak</h3>
</div>
</div>
Here is the code
const string url = "http://someurl.com/catalogue";
const string rootUrl = "http://someurl.com";
HtmlWeb hw = new HtmlWeb();
HtmlDocument doc = hw.Load(url);
int dealsCount = 0;
HtmlNode root = doc.DocumentNode.SelectSingleNode("//div[#id='dle-content']");
int i = 1;
//this is for the default page
while (i<=10)
{
try
{
string node= String.Format("//div[{0}]", i);
var link =
doc.DocumentNode.SelectSingleNode(node);
var href = link.SelectSingleNode("//div[#class='mlink']//span[#class='argmore']//a[#href]").Attributes["href"].Value;
string title = link.SelectSingleNode("//h3[#class='btl']//a[#href]").InnerText.Trim();
string description = link.SelectSingleNode("//div[#class='maincont']//div[1]").InnerText.Replace("\n", " ").Replace("\r", "").Replace("\t", "").Trim();
description = RemoveHTMLComments(description);
var imageURL = link.SelectSingleNode("//div[#class='maincont']//div[1]//a//img").Attributes["src"].Value;
var price = link.SelectSingleNode("//div[#class='mlink']//span[3]//font").InnerText.Trim();
price = Regex.Match(price, #"\d+").Value;
var partnerdealID = href;
//no information
var isActivesStr = link.SelectSingleNode("//div[#class='mlink']//span[2]/font").InnerText.Trim();
bool isActive;
if (isActivesStr.Contains("Нет в наличии"))
{
isActive = false;
}
else
{
isActive = true;
}
var dealUrl = href; //requires login - show the page itself
}
catch (Exception)
{
}
i += 1;
}
But after looping still the selected node is first one. What am I doing wrong?
All your XPATH expressions start with '//' which means "start from root of the document and search recursively". So when you do this:
link.SelectSingleNode("//div[#class='mlink']//span[#class='argmore']//a[#href]")
You will start not from link, but from the document's root. You probably want to do this instead:
link.SelectSingleNode("div[#class='mlink']...etc...")
which is equivalent to
link.SelectSingleNode("./div[#class='mlink']...etc...")
'.' means the current node. '/' means search only the direct children, not recursively.