Rendering partial view ASP MVC - asp.net-mvc

I want to render a partial view within the main view.
This is what I have done so far, but non of the code not loading the partial view.
Can anyone help me with this? Thanks in advance.
This is my main model
public class AppRequest
{
[Key]
public int Id { get; set; }
[Required]
[Display(Name = "Request Type")]
public int ReqType { get; set; }
[Required]
[Display(Name = "Requesting By")]
public int Req_By { get; set; }
[Required]
[Display(Name = "Requesting Date")]
public DateTime Req_Date { get; set; } = DateTime.Now;
[Required]
[Display(Name = "Request Location")]
public int Req_Location { get; set; }
[Required]
[Display(Name = "Request Heading")]
public string Req_Heading { get; set; }
[Display(Name = "Cover Note")]
public string Req_CoverNote { get; set; }
[Required]
[Display(Name = "Company Name")]
public int Company_Id { get; set; }
public virtual IList<Purchase> Purchase { get; set; }
public virtual IList<General> General { get; set; }
[NotMapped]
public Purchase PurchaseItem { get; set; }
}
#region Purchase
public class Purchase
{
[Key]
public int Id { get; set; }
[Required]
[ForeignKey("AppRequest")]
public int Req_Id { get; set; }
public virtual AppRequest AppRequest { get; set; }
public virtual IList<PurchasingEmpl> PurchasingEmpl { get; set; }
public virtual IList<PurchasingItems> PurchasingItems { get; set; }
}
public class PurchasingEmpl
{
[Key]
public int Id { get; set; }
[Required]
[ForeignKey("Purchase")]
public int Purchase_Id { get; set; }
public virtual Purchase Purchase { get; set; }
public int Emp_Id { get; set; }
public bool Status { get; set; }
}
public class PurchasingItems
{
[Key]
public int Id { get; set; }
[Required]
[ForeignKey("Purchase")]
public int Purchase_Id { get; set; }
public virtual Purchase Purchase { get; set; }
public int Supp_Id { get; set; }
public int Itm_Description_Id { get; set; }
public decimal Unit_Amount { get; set; }
public byte[] Attachment { get; set; }
public decimal Qty { get; set; }
public string Recomandation { get; set; }
public bool Status { get; set; }
public bool Settled { get; set; } = false;
public string PoNo { get; set; }
}
#endregion
And this is the View. My partial view name is "_Purchasing". So under this _Purchasing partial view, I have added another 2 partial views. So I need to load this main partial view to show the other details.
#model Asp_PASMVC.Models.AppRequest
#{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>AppRequest</h4>
<hr />
<div id="PurchseReq">
#Html.Partial("_Purchasing");
</div>
</div>
}

you can use #RenderPartial and #Partial
#RenderPartial vs #Partial
As mentioned, you can incorporate a partial view in a Razor view by using either of two methods: #RenderPartial or #Partial. The difference between the two methods may look small and harmless, but it may bite at you if you don’t know how to handle it.
The key difference between the two methods is that Partial returns a HTML-encoded string while #RenderPartial is a void method that writes directly to the response output stream. The usage of the two methods is slightly different:
#Html.Partial("_yourPartialView")
#Html.RenderPartial("_yourPartialView")

Related

How to insert an item into the collection with ef core

It's a class where i need to add item in Subtasks with Ef Core. Have no idea how to do it. Please help!
public class Do
{
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
public string Executors { get; set; }
public DoStatus Status { get; set; }
public DateTime Created { get; set; } = DateTime.Now;
[Required]
public int Plan { get; set; }
public int Fact { get; set; }
public DateTime? Finished { get; set; }
public virtual ICollection<Do> SubTasks { get; set; } = new List<Do>();
}
you need to learn about the recursive CTE relationship
for the date configure there with Api
builder.Property(p => p.Created).HasDefaultValueSql("(newid())")
public class Task
{
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
public string Executors { get; set; }
public DoStatus Status { get; set; }
public DateTime Created { get; set; };
[Required]
public int Plan { get; set; }
public int Fact { get; set; }
public DateTime? Finished { get; set; }
public int DoId { get; set; } // id parent
public virtual List<Task> SubTasks { get; set; };
}
var id = 1 // id Do
var subTasks = db.Task.include(s => s.SubTasks).where(s => e.DoId = id).ToList()

How to add a link to related data MVC

I have customer model
public class Customer
{
public Customer()
{
this.SystemValues = new HashSet<SystemValue>();
}
public string Name { get; set; }
public Nullable<System.Guid> GUID { get; set; }
public int Id { get; set; }
public virtual ICollection<SystemValue> SystemValues { get; set; }
}
and systemValue model
public class SystemValue
{
public int CustomerId { get; set; }
public int SystemValueId { get; set; }
public Nullable<int> SystemValueCategoryId { get; set; }
public string SystemValueType { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string TextValue { get; set; }
public Nullable<int> IntValue { get; set; }
public Nullable<double> FloatValue { get; set; }
public byte[] BlobValue { get; set; }
public Nullable<System.DateTime> DateTimeValue { get; set; }
public Nullable<bool> BooleanValue { get; set; }
public Nullable<int> LookupValueId { get; set; }
public Nullable<int> LookupValueGroupId { get; set; }
public Nullable<bool> IsReadonly { get; set; }
public bool IsHidden { get; set; }
public int Id { get; set; }
public virtual Customer Customers { get; set; }
}
in which way I could show a link in CustomerView(CustomersController) foreach customer that redirect to the SystemValuesView(SystemValuesController) with related to this customer SystemValues?
I found out one way - redirect to this controller's action with parameter.
public ActionResult ViewSystemValues(int? id)
{
return RedirectToAction("Index", "SystemValues", new {id});
}
But I'm sure there must be smarter way.
#for(int i = 0; i < Model.yourcustomerlist.Count(); i++)
{
<tr>
<td>
<a class="btn btn-primary" href="#Url.Action("Index", "SystemValues", new { id = Model.yourcustomerlist[i].CustomerId })">
<b>Go to system values</b>
</a>
</td>
</tr>
}
I hope I understood you correctly.
This code should go in the view. The view should be strongly typed to a model.
Code is if you want a button that redirects to the index view of the SystemValues controller, with the CustomerId as input. You should change "yourcustomerlist" to the list containing the customer information. If it's not part of a table, remove the table-related tags (<td> and <tr>).

A required field that exists in the view will always make model.state false

I have the following model class:-
public class TMSServer_Validation
{
[Required]
public string ILOIP { get; set; }
}
and the following view :-
#model TMS.ViewModels.ServerJoin
<div >
<span class="f"> ILOIP</span>
#Html.EditorFor(model =>model.Server.ILOIP)
#Html.ValidationMessageFor(model =>model.Server.ILOIP)
</div>
and the following post action method:-
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ServerJoin sj, FormCollection formValues)
{
//code goes here..
repository.InsertOrUpdateServer(sj.Server, User.Identity.Name, sj.Resource.RESOURCEID);
repository.Save();
return RedirectToAction("Index");
and the following view model class:-
public class ServerJoin
{
public TMSServer Server { get; set; }
public Resource Resource { get; set; }
public Technology Technology { get; set; }
public ComponentDefinition ComponentDefinition { get; set; }
}
but whenever i want to edit an object the model state will be False, and the error message indicates that the ILOP field is required, although there is a value for it on the view?
So what is causing this problem ?
UPDATE
Th model class is :-
[MetadataType(typeof(TMSServer_Validation))]
public partial class TMSServer
{
public TMSServer()
{
this.TMSServers1 = new HashSet<TMSServer>();
this.TMSVirtualMachines = new HashSet<TMSVirtualMachine>();
}
public int ServerID { get; set; }
public Nullable<int> ServerModelID { get; set; }
public int DataCenterID { get; set; }
public string ILOIP { get; set; }
public int RackID { get; set; }
public Nullable<int> StatusID { get; set; }
public Nullable<int> BackUpStatusID { get; set; }
public int RoleID { get; set; }
public Nullable<int> OperatingSystemID { get; set; }
public Nullable<int> VirtualCenterID { get; set; }
public string Comment { get; set; }
public byte[] timestamp { get; set; }
public long IT360SiteID { get; set; }
public virtual DataCenter DataCenter { get; set; }
public virtual OperatingSystem OperatingSystem { get; set; }
public virtual ServerModel ServerModel { get; set; }
public virtual Technology Technology { get; set; }
public virtual TechnologyBackUpStatu TechnologyBackUpStatu { get; set; }
public virtual TechnologyRole TechnologyRole { get; set; }
public virtual TechnologyStatu TechnologyStatu { get; set; }
public virtual TMSRack TMSRack { get; set; }
public virtual ICollection<TMSServer> TMSServers1 { get; set; }
public virtual TMSServer TMSServer1 { get; set; }
public virtual ICollection<TMSVirtualMachine> TMSVirtualMachines { get; set; }
}

Linq statement to select rows from db and display as list in MVC view

I need to select all rows from a table with a status as closed, and then I need to display results in a list in a asp.net mvc view.
What should I place in the cshtml first line?
#model HelpDesk.Domain.Entities.Ticket
#{
ViewBag.Title = "Help Desk | Admin Console";
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.selectedSub = 0;
}
How about here.
#using (Html.BeginForm())
{
}
Here is the method.
public Tickets GetTicketByStatus(string status)
{
return context.dbentity.Where()........
}
Here is the model
public class Ticket
{
public int CaseID { get; set; }
public string Title { get; set; }
[Required]
public string UFirstName { get; set; }
[Required]
public string ULastName { get; set; }
//public string UDisplayName { get; set; }
[Required]
public string UDep_Location { get; set; }
[Required]
public string UEmailAddress { get; set; }
//public string UComputerName { get; set; }
//public string UIPAddress { get; set; }
[Required]
public string UPhoneNumber { get; set; }
[Required]
public string Priority { get; set; }
[Required]
public string ProbCat { get; set; }
//public string IniDateTime { get; set; }
//public string UpdateProbDetails { get; set; }
//public string UpdatedBy { get; set; }
public string InitiatedBy_tech { get; set; }
public string AssignedBy { get; set; }
[Required]
public string TechAssigned { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public string ProbDetails { get; set; }
[Required]
public string TicketStatus { get; set; }
public string TicketSource { get; set; }
}

Display value in label using values in different table in razor view

I am looking to display Hole_Num value from my Holes class, in my RoundDetails class within the RoundDetails/Create razor view.
Here is my Holes class:
public partial class Hole
{
public Hole()
{
this.RoundDetails = new HashSet();
}
public int HoleId { get; set; }
public int FacilityId { get; set; }
public int CourseId { get; set; }
public int TeeTypeId { get; set; }
public int Hole_Num { get; set; }
public int Yardage { get; set; }
public byte Par { get; set; }
public short Handicap { get; set; }
public virtual ICollection<RoundDetail> RoundDetails { get; set; }
public virtual TeeType TeeType { get; set; }
}
Here is my RoundDetails class:
public partial class RoundDetail
{
public int RoundId { get; set; }
public int GolferId { get; set; }
public int FacilityId { get; set; }
public int CourseId { get; set; }
public int TeeTypeId { get; set; }
public int HoleId { get; set; }
public decimal Differential { get; set; }
public Nullable Date { get; set; }
public byte Score { get; set; }
public Nullable Putts { get; set; }
public Nullable GIR { get; set; }
public Nullable FIR { get; set; }
public Nullable Up_And_Down { get; set; }
public Nullable Par_Save { get; set; }
public Nullable Sand_Save { get; set; }
public Nullable Driving_Distince { get; set; }
public Nullable Proximity { get; set; }
public Nullable Green_Fee { get; set; }
public virtual Course Course { get; set; }
public virtual Facility Facility { get; set; }
public virtual Golfer Golfer { get; set; }
public virtual Hole Hole { get; set; }
public virtual TeeType TeeType { get; set; }
}
Here is my razor view:
<td>
#Html.LabelFor(model => model.Hole.Par)
</td>
How am I able to populate this label with the Par information from the Holes table? I assume that some manipulation with the Create method of the RoundDetailController or creating a ViewModel of some sort is the way to go but I"m not sure which one or how start either. Please help!

Resources