how to update a raw within a partial view in MVC C# - c#

i have a web application where i need to update the rows of a table one by one inside a partial view. the problem i'm facing now is how can i insert the update inside the partial view method in the controller. i'm using visual studio 2013 toad for oracle
The partial view
<table>
<thead>
<tr>
<th style="display:none;"> ProID </th>
<th> Requeser Name </th>
<th> Project Name </th>
<th> Supervisor Name </th>
<th> Description </th>
<th> Start Date </th>
<th> Due Date </th>
<th> Status </th>
<th> Done </th>
</tr>
</thead>
<tbody>
<tr>
#foreach (var items in Model)
{
<td>#items.USERID</td>
}
<td>
<input type="text" id="txt_pname" />
</td>
<td>
<input type="text" id="txt_sname" />
</td>
<td>
<input type="text" id="txt_dname" />
</td>
<td> <input name="text_stdate" id="iDate" class="form-control" readonly>
<span class="input-group-btn">
<button class="btn default" type="button"><i class="fa fa-calendar"></i></button>
</span></td>
<td>
<input name="text_dudate" id="iDate" class="form-control" readonly>
<span class="input-group-btn">
<button class="btn default" type="button"><i class="fa fa-calendar"></i></button>
</span>
</td>
<td>
<input type="text" id="txt_dname" />
</td>
<td>
<select id="txt_stat">
<option>Pending</option>
<option>Open</option>
<option>Close</option>
<option>Cancle</option>
</select>
</td>
</tr>
#foreach (var items in Model)
{
<td>
<a onclick="update_Row('#items.PROJECTT', '#items.IDP');" class="edit" href="#"> Done </a>
</td>
}
</table>
<table class="" id="sample_editable_1">
<thead>
<tr>
<th style="display:none;"> ProID </th>
<th> Requeser Name </th>
<th> Project Name </th>
<th> Supervisor Name </th>
<th> Description </th>
<th> Start Date </th>
<th> Due Date </th>
<th> Status </th>
<th> Edit </th>
</tr>
</thead>
<tbody>
#foreach (var items in Model)
{
<tr>
<td style="display:none;"> #items.IDP</td>
<td>#items.USERID</td>
<td>#items.PROJECTT</td>
<td>#items.PROJSUP</td>
<td>#items.PROJDES</td>
<td>#items.STARTDT</td>
<td>#items.DUEDT</td>
<td>#items.SCONDITION</td>
<td>
<a onclick="update_Row('#items.PROJECTT', '#items.IDP');" class="edit" href="#"> Edit </a>
</td>
</tr>
}
</tbody>
</table>
the controller
public ActionResult viewstat(string id)
{
List<mdlProject> itemList = new List<mdlProject>();
try
{
dbconn.openConnection();
strSQL = #"select ID,PROJTTL,PROJSVSOR,PROJDES,STARTDT,DUEDT,USERID,S_CONDITION from
PR_TBLPROJECT
inner join PR_TBLSTATUS on pr_tblstatus.s_id=PR_TBLPROJECT.id
where PR_TBLPROJECT.id=:id";
cmd = new OracleCommand(strSQL, dbconn.DbConn);
cmd.Parameters.Add("PROJTTL", OracleDbType.Varchar2, id, ParameterDirection.Input);
OracleDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
itemList.Add(new mdlProject
{
PROJECTT = dr["PROJTTL"].ToString(),
PROJSUP = dr["PROJSVSOR"].ToString(),
PROJDES = dr["PROJDES"].ToString(),
STARTDT = Convert.ToDateTime(dr["STARTDT"].ToString()).ToString("dd-MM-yyyy"),
DUEDT = Convert.ToDateTime(dr["DUEDT"].ToString()).ToString("dd-MM-yyyy"),
USERID = dr["USERID"].ToString(),
SCONDITION = dr["S_condition"].ToString(),
IDP = dr["ID"].ToString(),
//SID = dr["S_ID"].ToString(),
ACTV = "",
});
}
}
dr.Dispose();
cmd.Dispose();
}
finally
{
dbconn.closeConnection();
}
return PartialView(itemList);
}

Not sure what exact error you are getting but approach will be On click of each anchor tag, call a javascript method and from that javascipt method send a ajax request to update the row and then call your partial view method viewstat to refresh the table.

Related

How to include a copy button in Razor pages

I'm new to .Net Razor pages and followed this tutorial by FreeCodeCamp
I made an Index page with the help of the tutorial and wanted to add a copy button beside the entries but can't find a way to do so, here is the image of how the table looks, and here is the code:
//Index.cshtml.cs
private readonly ApplicationDbContext _db;
public IndexModel(ApplicationDbContext db)
{
_db = db;
}
public IEnumerable<PasswordEntry> PasswordEntries { get; set; }
public async Task OnGet()
{
PasswordEntries = await _db.PasswordEntry.ToListAsync();
}
//Index.cshtml
<br />
<div class="container row p-0 m-0">
<div class="col-9">
<h4>Passwords</h4>
</div>
</div>
<div class="col-12 border p-3 mt-3">
<form method="post">
#if (Model.PasswordEntries.Count() > 0)
{
<table class="table table-striped border">
<tr class="table-secondary">
<th>
<label asp-for="PasswordEntries.FirstOrDefault().Name"></label>
</th>
<th>
<label asp-for="PasswordEntries.FirstOrDefault().Url"></label>
</th>
<th>
<label asp-for="PasswordEntries.FirstOrDefault().Email"></label>
</th>
<th>
<label asp-for="PasswordEntries.FirstOrDefault().Username"></label>
</th>
<th>
<label asp-for="PasswordEntries.FirstOrDefault().Password"></label>
</th>
<th>
</th>
</tr>
#foreach (var item in Model.PasswordEntries)
{
<tr>
<td>
#Html.DisplayFor(m => item.Name)
</td>
<td>
#Html.DisplayFor(m => item.Url)
</td>
<td>
#Html.DisplayFor(m => item.Email)
</td>
<td>
#Html.DisplayFor(m => item.Username)
</td>
<td>
#Html.TextBoxFor(m => item.Password, new { type = "password", disabled = "disabled" })
</td>
<td>
<button asp-page-handler="Delete" asp-route-id="#item.Id">Delete</button>
<a asp-page="Edit" asp-route-id="#item.Id">Edit</a>
</td>
</tr>
}
</table>
}
else
{
<h1>no entries available</h1>
}
</form>
</div>
I did try adding a copy button beside the delete and edit buttons with onClick function, on click it would call a funcition in #section Script area, but it didn't work probably cause the foreach loop ends after displaying the result,
what would be a good way to implement the copy button?
If you want to copy the line when click Copy button,here is a demo:
<div class="col-12 border p-3 mt-3">
<form method="post">
#if (Model.PasswordEntries.Count() > 0)
{
<table class="table table-striped border">
<tr class="table-secondary">
<th>
<label asp-for="PasswordEntries.FirstOrDefault().Name"></label>
</th>
<th>
<label asp-for="PasswordEntries.FirstOrDefault().Url"></label>
</th>
<th>
<label asp-for="PasswordEntries.FirstOrDefault().Email"></label>
</th>
<th>
<label asp-for="PasswordEntries.FirstOrDefault().Username"></label>
</th>
<th>
<label asp-for="PasswordEntries.FirstOrDefault().Password"></label>
</th>
<th>
</th>
</tr>
#foreach (var item in Model.PasswordEntries)
{
<tr>
<td>
#Html.DisplayFor(m => item.Name)
</td>
<td>
#Html.DisplayFor(m => item.Url)
</td>
<td>
#Html.DisplayFor(m => item.Email)
</td>
<td>
#Html.DisplayFor(m => item.Username)
</td>
<td>
#Html.TextBoxFor(m => item.Password, new { type = "password", disabled = "disabled" })
</td>
<td>
<button asp-page-handler="Delete" asp-route-id="#item.Id">Delete</button>
<button onclick="copy(this)">Copy</button>
<a asp-page="Edit" asp-route-id="#item.Id">Edit</a>
</td>
</tr>
}
</table>
}
else
{
<h1>no entries available</h1>
}
</form>
</div>
js:
#section Scripts{
<script>
function copy(t) {
$("table")[0].innerHTML += $(t).parent().parent()[0].outerHTML;
}
</script>
}
result:
Update:
If you want to copy password to clipboard.Try to use the folowing js function:
function copy(t) {
var password = $(t).parent().parent()[0].children[4].children[0].value;
navigator.clipboard.writeText(password);
}

Pass An int value from view to Action from a link click

I have a for loop in my view about a table. I am able to display the results. but when i click any of the links . i get a null point exception. i found that it is because that the loop is finished
#model List<MyMainModel>
#using System.Collections;
#using System.Web;
#{
ViewData["Title"] = "Display";
}
<h1>Display</h1>
<table border="1" cellpadding="20" style="table-layout:fixed">
<tr>
<th>
UserName
</th>
<th>
PAssword
</th>
<th>
Email
</th>
<th colspan="2">
Options
</th>
</tr>
#{ for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
#Model.ElementAt(i).USERNAME
</td>
<td>
#Model.ElementAt(i).PASSWORD
</td>
<td>
#Model.ElementAt(i).EMAIL
</td>
<td >
Remove
</td>
<td>
Edit
</td>
</tr>
}
}
</table>
When i click Remove i want to pass ID of the corresponding Element back to RemoveRow Action how do i do that
#Url.Action("RemoveRow", "MyMain", new { id = i })

Use foreign key table columns to display data asp.net core mvc c#

I have an Invoice Class with FoodId and UserId foreign keys to FoodItem and User table.
I have the following razor page Index.cshtml from adding a scaffolded item to the controllers folder.
#model IEnumerable<WebApplication1.Models.Invoice>
#{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<link rel="stylesheet" href="~/css/SideBar.css" />
<div class="sidebar">
<a asp-controller="Admin">Main Page</a>
<a asp-controller="FoodItems">Food List</a>
<a class="active" asp-controller="Invoices">Invoice</a>
<a asp-controller="Role">Users & Roles</a></div>
<h1>Invoice</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.UserId)
</th>
<th>
#Html.DisplayNameFor(model => model.FoodId)
</th>
<th>
#Html.DisplayNameFor(model => model.totalsum)
</th>
<th>
#Html.DisplayNameFor(model => model.quantity)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.UserId)
</td>
<td>
#Html.DisplayFor(modelItem => item.FoodId)
</td>
<td>
#Html.DisplayFor(modelItem => item.totalsum)
</td>
<td>
#Html.DisplayFor(modelItem => item.quantity)
</td>
<td>
<a asp-action="Edit" asp-route-id="#item.InvoiceId">Edit</a> |
<a asp-action="Details" asp-route-id="#item.InvoiceId">Details</a> |
<a asp-action="Delete" asp-route-id="#item.InvoiceId">Delete</a>
</td>
</tr>
}
</tbody>
</table>
I want to list instead of the FoodId and UserId , bring the columns foodname and username from the FoodItem and UserId tables.
How can I achieve that?
When you retrieve Invoice entity include food and user like this:
_context.Invoices.Include(x => x.Users).Include(x => x.Food).ToList();
And then in your view write like this ;
#Html.DisplayNameFor(model => model.User.Name)
#Html.DisplayNameFor(model => model.Food.Name)

Generate Rotativa PDF after LINQ search

I'm using MVC 4 and using rotativa PDF to generate a pdf from a view.
The problem is that the view in question is a result of a LINQ search, and in the pdf it doesn't appear the data.
This is where the user fill the box with what he wants to search:
First
After he clicks "Pesquisar"(search) this view is created:
Second
But when the pdf is generated, it doesn't show any data.
What can i do or what is the error?
The code:
Controller (the LINQ code works):
public ActionResult ViewAfterSearch(string texto)
{
var util = (db.Simulacaos.Where(m =>
m.Utilizador.Contains(texto)));
return View("ViewAfterSearch", util);
}
public ActionResult ViewBeforeSearch()
{
return View(db.Simulacaos.ToList());
}
[HttpPost, ActionName("ViewBeforeSearch")]
public ActionResult ViewBeforeSearch(string texto)
{
var util = (db.Simulacaos.Where(m =>
m.Utilizador.Contains(texto)));
return View("ViewAfterSearch", util);
}
Views:
#model IEnumerable<GestorSalas.Models.Simulacao>
#{
ViewBag.Title = "Registos";
}
<h2>ViewAfterSearch</h2>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Utilizador)
</th>
<th>
#Html.DisplayNameFor(model => model.TipoUtilizador)
</th>
<th>
#Html.DisplayNameFor(model => model.Codigo)
</th>
<th>
#Html.DisplayNameFor(model => model.Hora)
</th>
<th>
Entrou?
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Utilizador)
</td>
<th>
#Html.DisplayFor(modelItem => item.TipoUtilizador)
</th>
<td>
#Html.DisplayFor(modelItem => item.Codigo)
</td>
<td>
#Html.DisplayFor(modelItem => item.Hora)
</td>
<td>
#Html.DisplayFor(modelItem => item.Entrar)
</td>
</tr>
}
</table>
<h4>#Html.ActionLink("Download como PDF", "TurnToPDF", "Simulacao")</h4>
<h4>#Html.ActionLink("Voltar", "Index", "Simulacao")</h4>
View before search:
#model IEnumerable<GestorSalas.Models.Simulacao>
#{
ViewBag.Title = "Index";
}
<h4>Procurar registo de utilizadores</h4>
using (Html.BeginForm("ViewAfterSearch", "Simulacao", FormMethod.Post))
{
<table>
<tr>
<th>
#Html.TextBox("texto")
</th>
<td>
<input type="submit" id="ButtonProcura" value="Procurar" />
</td>
</tr>
</table>
}
#Html.ActionLink("Download como PDF", "TurnToPDF", "Simulacao")
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Utilizador)
</th>
<th>
#Html.DisplayNameFor(model => model.TipoUtilizador)
</th>
<th>
#Html.DisplayNameFor(model => model.Codigo)
</th>
<th>
#Html.DisplayNameFor(model => model.Hora)
</th>
<th>
Entrou?
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Utilizador)
</td>
<th>
#Html.DisplayFor(modelItem => item.TipoUtilizador)
</th>
<td>
#Html.DisplayFor(modelItem => item.Codigo)
</td>
<td>
#Html.DisplayFor(modelItem => item.Hora)
</td>
<td>
#Html.DisplayFor(modelItem => item.Entrar)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.SimulacaoId }) |
#Html.ActionLink("Details", "Details", new { id=item.SimulacaoId }) |
#Html.ActionLink("Delete", "Delete", new { id=item.SimulacaoId })
</td>
</tr>
}
</table>
}

C# ASP.NET MVC Table rows as form values FormCollection

I'm stuck with getting values out of the FormCollection in my controller. The problem is that my form is a table.
<% using (Html.BeginForm("PasOverlappendeAfwezighedenAan", "Afwezigheid")) {%>
<table>
<tr>
<th></th>
<th>
datumVan
</th>
<th>
datumTot
</th>
<th>
beginUur
</th>
<th>
eindUur
</th>
<th>
reden
</th>
</tr>
<% foreach (var item in Model.Afwezigheden)
{ %>
<tr>
<td>
<input type="text" name="datumVan" id="datumVan" value="<%: String.Format("{0:g}", item.datumVan) %>" />
</td>
<td>
<input type="text" name="datumTot" id="Text1" value="<%: String.Format("{0:g}", item.datumTot) %>" />
</td>
<td>
<input type="text" name="beginUur" id="Text2" value="<%: item.beginUur %>" />
</td>
<td>
<input type="text" name="eindUur" id="Text3" value="<%: item.eindUur %>" />
</td>
<td>
<input type="text" name="reden" id="Text4" value="<%: item.reden %>" />
</td>
</tr>
<% } %>
</table>
<% } %>
So the Controller should turn each row from the table into an object.
public ActionResult PasOverlappendeAfwezighedenAan(FormCollection fc, Student stud){
}
But how would I get a row or the data for one row out of the FormCollection?
Thanks in advance,
Me
ps: sorry for the dutch names in the codes
sure we can use the form colleciton like:
public ActionResult Test2(FormCollection postback)
{
var i = postback["beginUur"];
var j = postback["eindUur"];
return View();
}
then the value of i will be a string like: "20,40,60" if our post form has 3 fields(or called [input text]) is named "beginUur".. and we can use
string [] beginUurResult = i.Split(',');
to get the string array of each beginUur value
--
or each field be a single List like:
public ActionResult PasOverlappendeAfwezighedenAan(List<string> datumVan, List<string> datumTot){
}

Categories