I have this ActionLink into view named catalog
#Html.ActionLink("Formations", "Index", "Formation", new { id = item.Id },null)
and I want to get the list of formations of this catalog id.
How can I do this ?
In my database I have two tables :
Catalog:idC (PK),NameC,Date
Formation:idF(PK),idC(FK),NameF.
If your view's name is Catalog, you need to change your ActionLink to point to the corresponding action:
#Html.ActionLink("Formations", "Catalog", "Formation", new { id = item.Id }, null)
action:
public ActionResult Catalog(int id)
{
var formations = db.Formations.Where(f => f.idC == id).ToList;
return View(formations);
}
view:
#model IEnumerable<ProjectName.Models.Formation>
// loop your formations
#foreach (var item in Model)
{
// each formation iteration loops here
}
Related
I'm new to mvc 4 and having trouble with loading profile after click on hyperlink in View.I'm using view model for combining several classes to one class. Below I h'v added View of Index.
<h3>Place</h3>
#foreach (var item in Model)
{
<div class="item1">
<img src="data:image/png;base64,#Convert.ToBase64String(item.pic,0,item.pic.Length)" width="100" />
<p class="name">#Html.ActionLink(item.Sp_name, "Details", new { id = item.SPID })</p>
</div>
}
once I click on hyperlink in Index View. I want to load it's profile.I'm having problem with where to add find(id) in controller. given below is my controller method for View profile.
public ActionResult Details(int id = 0)
{
List<ImageData> details = new List<ImageData>();
var sp_details = (from s in db.service_provider
join p in db.pictures on s.SPID equals p.SPID
join c in db.cities on s.City_ID equals c.City_ID
where s.SPID == id
select new { s.Sp_name, s.Sp_location, s.Sp_rate, s.service_type, c.Cityname, p.pic });
foreach (var item in sp_details)
{
ImageData SpView = new ImageData(); // ViewModel
SpView.Sp_name = item.Sp_name;
SpView.Sp_location = item.Sp_location;
SpView.Cityname = item.Cityname;
SpView.Sp_rate = item.Sp_rate;
SpView.pic = item.pic;
details.Add(SpView);
}
if (details == null)
{
return HttpNotFound();
}
return View(details);
}
can somebody kindly help me to load profile after click on hyperlink in view.
Try this :
#Html.ActionLink(item.Sp_name, "Details", new { id = item.SPID },null)
i'm a total beginner in asp.net mvc and am trying to change the items in a dropdownlist according to the item selected in an author one (they are filled from a database)
<% using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "TheForm" }))
{
Filiere: <%= Html.DropDownList("filiere", (SelectList)ViewData["filiere"], new { onchange = "this.form.submit();" })%>
Module:<%= Html.DropDownList("module",(SelectList)ViewData["module"])
}
%>
public ActionResult Index(int? fil)
{
var fi = db.filiere.Select(f => new {f.id,f.nom });
ViewData["filiere"] = new SelectList(fi.AsEnumerable(), "id", "nom");
List<module> mod;
if (fil == null)
mod = db.module.ToList();
else
{
mod = (from module in db.module
where module.id_filiere == fil
select module).ToList();
}
ViewData["module"] = new SelectList(mod.AsEnumerable(),"id","nom");
return View();
}
Try to create one more method in controller who accept the author information and return the list which you want to display in drop-down.
then simply put ajax call on author change event call the controller method for data and also pass current author information and on success bind result what you get from controller method to drop-down.
Getting 'null' from List View to Edit/Details/Delete Action method instead of the Id.
In List view, in Id column it shows corresponding Id without any issues. In All.cshtml file,
<td>
#Html.DisplayFor(modelItem => item.ModifiedOn)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.CategoryId }) |
#Html.ActionLink("Details", "Details", new { id = item.CategoryId }) |
#Html.ActionLink("Delete", "Delete", new { id = item.CategoryId })
</td>
And Edit method is,
public ActionResult Edit(int? id) {
if (id == null) {
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
return View();
}
var editCategory = new PetaPoco.Database("DefaultConnection");
var category = editCategory.Single<CategoryViewModels>("SELECT * FROM Category WHERE
CategoryId=#0 AND IsActive = 1", id);
return View(category);
}
The Url in the browser is, /Category/Edit/C1. But in Edit/Details/Delete, the Id is null.
What am I missing?
Thanks.
Since the url can be /Category/Edit/C1, the id parameter in the controller action method can't be an int?. Try to change the type of id to string
public ActionResult Edit(string id) {
if (string.IsNullOrEmpty(id)) {
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
return View();
}
var editCategory = new PetaPoco.Database("DefaultConnection");
var category = editCategory.Single<CategoryViewModels>("SELECT * FROM Category WHERE
CategoryId=#0 AND IsActive = 1", id);
return View(category);
}
the "C1" is not type of int, convert it to "int?" will get null.
Part of ControllerA:
public ActionResult Index()
{
ViewBag.Message = "ToolScope Testing";
var Baselines = from b in db.Baselines
orderby b.Name
select b;
ViewBag.Baselines = Baselines;
return View();
}
Part of View for ControllerA
#foreach (var item in #ViewBag.Baselines)
{
<tr>
<li> #Html.ActionLink( item.Name, "Details", "BaseLine",new { id = item.BaselineID }, null) </li>
</tr>
}
The item.Name is causing problem, however, it works if I use something like
<li> #Html.ActionLink( "SomeName", "Details", "BaseLine",new { id = item.BaselineID }, null) </li>
What should I do to have the dynamic names, i.e., the first ActionLink?
P.S.: I am new to MVC
I see you are new to MVC. Good news, you've already gotten the V(iew) and the C(ontroller). Now it's time to master the M(odel). In your example, you are using the ViewBag to transport knowledge from the Controller to your View. This is a typical responsibility of the Model. So you need to create a new class in your Models directory. It will probably look something like this:
public class MyFirstModel
{
public IEnumerable<MyCustomType> Baselines { get; set; }
public MyFirstModel() { }
}
Edit your Controller
public ActionResult Index()
{
ViewBag.Message = "ToolScope Testing";
var baselines = from b in db.Baselines
orderby b.Name
select b;
var model = new MyFirstModel
{
Baselines = baselines
};
return View(model);
}
Then, add this to the top of your View:
#model MvcApplication.Models.MyFirstModel
Now you can use this code in your view instead:
#foreach (var item in Model.BaseLines)
{
<tr>
<li> #Html.ActionLink( item.Name, "Details", "BaseLine",new { id = item.BaselineID }, null) </li>
</tr>
}
I'm having trouble sorting a list of user profiles which I am passing to the view. I want to display the list of all users in a certain role and I want to sort them by familyName attribute.
I tried using OrderBy but it has no effect.
Code in the controller
public ActionResult Index()
{
//get all patients
var patients = Roles.GetUsersInRole("user").ToList();
//set up list of patient profiles
List<UserProfile> pprofiles = new List<UserProfile>();
foreach (var i in patients) {
pprofiles.Add(ZodiacPRO.Models.UserProfile.GetUserProfile(i));
}
pprofiles.OrderBy(x => x.familyName); //<-this has no effect the list produced is
// exactly the same it was without this line
return View(pprofiles);
}
And the View
<ul id= "patientList">
#foreach (var m in Model)
{
<li>
<ul class="patient">
<li class="ptitle">#m.title</li>
<li class="pname"> #Html.ActionLink(#m.givenName + " " + #m.familyName, "View", "Account", new { #username = #m.UserName.ToString() }, new { id = "try" })</li>
<li class="pprofile">#Ajax.ActionLink("Profile", "PatientSummary", new { #username = #m.UserName }, new AjaxOptions { UpdateTargetId = "pContent"},new{ #class = "profpic" })</li>
</ul>
</li>
}
</ul>
I will need to reuse this in more than one place and there could be a large number of users so not ordering them in someway would be terrible. How should I go about this?
pprofiles.OrderBy(x => x.familyName); will return an IEnumerable<T>, not sorting the array where it was called on.
You can change your code like this :
public ActionResult Index()
{
//get all patients
var patients = Roles.GetUsersInRole("user").ToList();
//set up list of patient profiles
List<UserProfile> pprofiles = new List<UserProfile>();
foreach (var i in patients) {
pprofiles.Add(ZodiacPRO.Models.UserProfile.GetUserProfile(i));
}
var ordered = pprofiles .OrderBy(x => x.familyName);
return View(ordered );
}
Or in a more Linq-styled way :
var orderedPatients = Roles.GetUsersInRole("user")
.Select(u=>ZodiacPRO.Models.UserProfile.GetUserProfile(u))
.OrderBy(u=>u.FamilyName);
return View(orderedPatients);
Or :
var orderedPatients = from u in Roles.GetUsersInRole("user")
let userProfile = ZodiacPRO.Models.UserProfile.GetUserProfile(u)
order by userProfile.FamilyName
select userProfile;
return View(orderedPatients);
OrderBy does not modify the order of pprofiles elements, rather it returns a new collection with the elements ordered. You can try this:
pprofiles = pprofiles.OrderBy(x => x.familyName);
Or you can use List(T).Sort
You need to assign it back to your variable, OrderBy returns sorted collection:
pprofiles = pprofiles.OrderBy(x => x.familyName);