String with email format turns into Hyperlink - c#

Hi this probably is an easy question but i cant find information about how to solve it i have a table with a field named Email and the values are of type string but the problem is that mvc or the browser automatically changes that string email into Hyperlink as shown in the following picture
when i inspect the element it is an hyperlink:
lacubana#la.com
what can i do to only display the emails as string? i don't want that information to be in hyperlink format. thanks very much
Edited: here is my code of the view
<table class="table table-bordered table-striped">
<tr>
<th>Email</th>
<th>Contraseña</th>
<th>NickName</th>
<th>TipoUsuario</th>
<th>Acciones</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>#Html.DisplayFor(modelItem => item.Email)</td>
<td>#Html.DisplayFor(modelItem => item.Contraseña)</td>
<td>#Html.DisplayFor(modelItem => item.NickName)</td>
#if (item.TipoUsuario == 1)
{
<td>Administrador</td>
}
else
{
<td>Vendedor</td>
}
<td>
#Html.ActionLink("Editar", "EditarUsuario", new { id = item.IdUser }) |
#Html.ActionLink("Eliminar", "EliminarUsuario", new { id = item.IdUser })
</td>
</tr>
}
</table>
and here is the code of my controller:
IList<Usuario> UsuarioList = new List<Usuario>();
var query = from usu in database.ReportingUsersT
where usu.Activo == 1
select usu;
var listdata = query.ToList();
foreach (var Usuariodata in listdata)
{
UsuarioList.Add(new Usuario()
{
IdUser = Usuariodata.IdUser,
Email = Usuariodata.Email,
Contraseña = Usuariodata.Contraseña,
NickName = Usuariodata.NickName,
TipoUsuario = Usuariodata.TipoUsuario
});
}
return View(UsuarioList);

#Html.DisplayFor(...) is determining that the text is an email and is wrapping it in a link. You can simply use
<td>#item.Email</td>
to display it as text

Related

How to find last column of a table using Html Agility Pack

I have a table like this:
<table border="0" cellpadding="0" cellspacing="0" id="table2">
<tr>
<th>Name
</th>
<th>Age
</th>
</tr>
<tr>
<td>Mario
</td>
<th>Age: 78
</td>
</tr>
<tr>
<td>Jane
</td>
<td>Age: 67
</td>
</tr>
<tr>
<td>James
</td>
<th>Age: 92
</td>
</tr>
</table>
I want to get the last td from all rows using Html Agility Pack.
Here is my C# code so far:
await page.GoToAsync(NumOfSaleItems, new NavigationOptions
{
WaitUntil = new WaitUntilNavigation[] { WaitUntilNavigation.DOMContentLoaded }
});
var html4 = page.GetContentAsync().GetAwaiter().GetResult();
var htmlDoc4 = new HtmlDocument();
htmlDoc4.LoadHtml(html4);
var SelectTable = htmlDoc4.DocumentNode.SelectNodes("/html/body/div[2]/div/div/div/table[2]/tbody/tr/td[1]/div[3]/div[2]/div/table[2]/tbody/tr/td[4]");
if (SelectTable.Count == 0)
{
continue;
}
else
{
foreach (HtmlNode row in SelectTable)//
{
string value = row.InnerText;
value = value.ToString();
var firstSpaceIndex = value.IndexOf(" ");
var firstString = value.Substring(0, firstSpaceIndex);
LastSellingDates.Add(firstString);
}
}
How can I get only the last column of the table?
I think the XPath you want is: //table[#id='table2']//tr/td[last()].
//table[#id='table2'] finds the table by ID anywhere in the document. This is preferable to a long brittle path from the root, since a table ID is less likely to change than the rest of the HTML structure.
//tr gets the descendent rows in the table. I'm using two slashes in case there might be an intervening <tbody> element in the actual HTML.
/td[last()] gets the last <td> in each row.
From there you just need to select the InnerText of each <td>.
var tds = htmlDoc.DocumentNode.SelectNodes("//table[#id='table2']//tr/td[last()]");
var values = tds?.Select(td => td.InnerText).ToList() ?? new List<string>();
Working demo here: https://dotnetfiddle.net/7I8yk1

C# MVC Download dynamic File Name

How can I download a pdf file that's not static.
I tried this:
public ActionResult GetPdf(string filename)
{
var invoice = db.Invoices.ToList().Find(x=>x.InvoicePath==filename);
return File("~/Invoice/" +invoice.ToString(), "application/pdf",Server.UrlEncode(invoice.ToString()));
}
The Index View
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.CreatedInvoice)
</th>
<th>
#Html.DisplayNameFor(model => model.InvoicePath)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.CreatedInvoice)
</td>
<td>
#Html.DisplayFor(modelItem => item.InvoicePath)
</td>
<td>
#Html.ActionLink("Download", "GetPdf", new { id = item.InvoicePath })
</td>
</tr>
}
</table>
This is my upload file method
public ActionResult fileupload(HttpPostedFileBase file)
{
if (file != null)
{
string ImageN = System.IO.Path.GetFileName(file.FileName);//get the file path
string physicalPath = Server.MapPath("~/Invoice/" + ImageN);//store the file path in a folder called img
file.SaveAs(physicalPath);
Invoice newP = new Invoice();
newP.InvoicePath = ImageN;
newP.CreatedInvoice = DateTime.Now;
db.Invoices.Add(newP);//store the image path in a database
db.SaveChanges();
return RedirectToAction("Index");
}
return View();
}
Any suggestions will be Greatly appreciated
This is what your code should be:
public ActionResult GetPdf(string filename)
{
var invoice = db.Invoices.FirstOrDefault(x=> x.InvoicePath == filename);
return File("~/Invoice/" + invoice.InvoicePath, "application/pdf", Server.UrlEncode(invoice.InvoicePath));
}
I'm not sure why you're Querying the Database though as you're getting the fileName in paramater and that is all you need to serve the file anyways.
You're saving the file name in InvoicePath property when file is uploaded, it is this property which you should be using to get the name of file to be downloaded.
I just Changed the download Method To This
public ActionResult GetPdf(int id)
{
var invoice = db.Invoices.Find(id);
return File("~/Invoice/" + invoice.InvoicePath, "application/pdf", Server.UrlEncode(invoice.InvoicePath));
}
and Changed The Action Link to
#Html.ActionLink("Download", "GetPdf", new { id = item.InvoiceId })
dont really know why the above method did not work

Updating Multiple Records in one view; "Not all code paths return a value" error

I'm really new to programming and stuck on a problem.
I'm trying to edit and update multiple rows of a database in one view, using mvc and asp.net.
I think I'm somewhere along the right tracks but keep getting an error saying "not all code paths return a value".
My Conroller looks like this:
[HttpGet]
public ViewResult AnotherListEdit()
{
var chosenClass = from c in db.ClassInstanceDetails.Include("ClassInstance").Include("Student")
where c.ClassInstance.ID == 1
select c;
return View(chosenClass.ToList());
}
[HttpPost]
public ActionResult AnotherListEdit(IList<ClassInstanceDetail> list)
{
if (ModelState.IsValid)
{
foreach (ClassInstanceDetail editedClassInstanceDetail in list)
{
var tempBook = (from classInstDet in db.ClassInstanceDetails
where (teacher.ClassInstanceID == editedClassInstanceDetail.ClassInstanceID)
&& (classInstDet.StudentID == editedClassInstanceDetail.StudentID)
select teacher).First();
db.ApplyCurrentValues(tempBook.EntityKey.EntitySetName, editedClassInstanceDetail);
}
db.SaveChanges();
return View(db.Teachers.ToList());
}
}
My View looks like this:
#model IList<FYPSchoolApp.DAL.ClassInstanceDetail>
#{
ViewBag.Title = "AnotherListEdit";
}
#using (Html.BeginForm())
{
<table>
<tr>
<th>
Name
</th>
<th>
Second Name
</th>
<th>
attendance
</th>
<th>
Comment
</th>
</tr>
#for (var i = 0; i < Model.Count(); i++) {
<tr>
<td>
#Html.DisplayFor(m => Model[i].StudentID)
</td>
<td>
#Html.DisplayFor(m => Model[i].Attendance)
#Html.EditorFor(m => Model[i].Attendance)
</td>
<td>
#Html.DisplayFor(m => Model[i].CommentNote)
#Html.EditorFor(m => Model[i].CommentNote)
</td>
</tr>
}
</table>
<input type="submit" value="save" />
}
The "not all code paths return a value error" is being highlighted with AnotherListEdit function, the second one thats after HttpPost. If I run the project without that whole function, the display works, and the correct information is passed to the display.
Any help would be very much appreciated!!
What should happen in the AnotherListEdit method if the modelstate is invalid? That is what is missing ... The action does not return a "ActionResult" if the modelstate is invalid
[HttpPost]
public ActionResult AnotherListEdit(IList<ClassInstanceDetail> list)
{
if (ModelState.IsValid)
{
foreach (ClassInstanceDetail editedClassInstanceDetail in list)
{
var tempBook = (from teacher in db.ClassInstanceDetails
where (teacher.ClassInstanceID == editedClassInstanceDetail.ClassInstanceID)
&& (teacher.StudentID == editedClassInstanceDetail.StudentID)
select teacher).First();
db.ApplyCurrentValues(tempBook.EntityKey.EntitySetName, editedClassInstanceDetail);
}
db.SaveChanges();
return View(db.Teachers.ToList());
}
//HERE!!What view should return? any error messages?
return View("View with no valid modelstate?");
//Maybe?
//return RedirectToAction("AnotherListEdit");
}
if (ModelState.IsValid)
{ //you return something here }
but if the modelstate is not valid, nothing is returned. The error must come from there

C# How I can retrieve this information?

On the HTML Page I have something like that
<table class="information">
<tbody>
<tr>
<td class="name">Name:</td>
<td>John</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
</tr>
....
</tbody>
</table>
How I can retrieve the name (there are other information too but in my example I wrote only name)?
Notes: HTML has more than one table
I tried this
foreach (HtmlElement item in wb.Document.GetElementsByTagName("table"))
{
if (item.OuterHtml.Contains("information"))
{
... //Here i don't know how to continue
}
}
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var table = doc.DocumentNode.SelectSingleNode("//table[#class='information']");
var td = table.SelectSingleNode("//td[#class='name']");
Console.WriteLine(td.InnerText);
or
var text = doc.DocumentNode.Descendants("td")
.First(td => td.Attributes["class"] != null && td.Attributes["class"].Value == "name")
.InnerText;
HtmlElementCollection tData = wb.Document.GetElementsByTagName("td");
foreach (HtmlElement td in tData)
{
string name = "";
if (td.GetAttribute("classname") == "name")
{
name = td.InnerText;
}
}
Check out HtmlAgilityPack - it is free and quite good library to work with html sources.

linq to entity query returns list as continuous string

I have a query that looks like this:
var ChangesOthersResult = surveyResponseRepository.Query.Select(r => r.ChangesOthers);
That returns all of the entries in "ChangesOthers" column from my table.
When I return the data inside my view:
#Html.DisplayFor(modelItem => modelItem.ChangesOthersResult)
It is returning all of the data as a single continous string of text. How can I add a line break in between the columns of data that it is returning?
var data = new ResultsViewModel()
{
PatientFollowUpResult = PatientFollowUpResult,
PatientFollowUpResultPct = PatientFollowUpResultPct,
TotalResponsesResult = TotalResponsesResult,
ChangesOthersResult = ChangesOthersResult,
};
View Model Type
#model CMESurvey.ViewModels.ResultsViewModel
Perhaps something like:
#foreach (var x in Model.ChangesOthersResult)
{
#x<br>
}
<table>
<tr>
<th>
HeaderName1
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => modeItem.FieldName)
</td>
</tr>
}
</table>
Razor doesn't know that you want line breaks in between each item when you use
#Html.DisplayFor(modelItem => modelItem.ChangesOthersResult)
You can literally add the line break in the select by adding the string "<br/>" to each item before selecting it:
var ChangesOthersResult = surveyResponseRepository
.Query
.Select(r => r.ChangesOthers + "<br />");

Categories