I am using MVC and have a view (test.cshtml) that contains a form. Is there a way to send it to another View page.cshtml for testing instead of same [http] controller ActionResult test()?
I am trying to validate that all the form field values are correct before updating db. Is there an easier way to do this?
In your view
#using (Html.BeginForm("Add", "Weight", FormMethod.Post))
//where "Add" is the Action name and Weight is the controller (WeightController) -> http://foo/Weight
{
......
}
With a model
public class WeightModel
{
[Required] public string Description { get; set; }
[Required] public int Weight { get; set; }
public string Notes { get; set; }
}
In your Controller
[HttpPost]
public ActionResult Add(WeightModel model)
{
if (!ModelState.IsValid) //framwork will validate based on attributes on model
{
return View("Index", model);
}
else
{
//save to db
return RedirectToAction("Added");
}
}
Related
I am having issue with DropDownList in MVC,
I can populate the Dropdownlist on HTTPGET i.e data entry page displays the Dropdown list with proper data from datatable, but as soon as I press Submit button MVC takes me on the Viewer line of DropDownList with the error "There is no ViewData item of type 'IEnumerable' that has the key 'Suburb'"
My Model is
public class ServiceEntryRegister
{
public List<ReceiptHeader> HEADER { get; set; }
public List<ReceiptDetail> DETAIL { get; set; }
public ReceiptHeader SingleHEADER { get; set; }
public ReceiptDetail SingleDETAIL { get; set; }
}
Controller
[HttpGet]
public ActionResult CreateNew()
{
ServiceEntryRegister Model = new ServiceEntryRegister();
ViewBag.Btype = new SelectList(_er.BodyTypes, "Bodytypeid", "btype");
ViewBag.Engine = new SelectList(_er.EngineTypes, "Enginetypeid", "Engine");
ViewBag.Suburb = new SelectList(_er.Suburbs, "SuburbID", "SuburbName");
ViewBag.VehicleMake = new SelectList(_er.VehicleMakes, "VehicleMakeID", "Make");
return View();
}
[HttpPost]
// [ValidateAntiForgeryToken]
public ActionResult CreateNew(ServiceEntryRegister RH)
{
if (ModelState.IsValid)
{
CreateBagForLists();
RH.SingleHEADER.ReceiptID = _HC.PrimaryKeyGenerater();
_er.ReceiptHeaders.Add(RH.SingleHEADER);
_er.SaveChanges();
return View();
}
else
{
return View();
}
}
The View is :
<td>#Html.DropDownList("Suburb", String.Empty)</td>
I have also tried:
<td>#Html.DropDownList("Suburb", ViewBag.Suburb as SelectList)</td>
but all in vain.
I would suggest to avoid viewbags and use strongly type dropdown list.
In your approach you have to bind viewbags in both actions (post and get) to overcome this error.
I'm trying to list the items from my database into my view but I'm getting null back.
I know the connection must be working to a certain extent because in my database the tables didn't exist but once I ran my program it did create the tables. However when I add content into my table my view still returns NULL.
Also, haven't touched the Review table yet, just worried about getting Restaurants working.
Restaurant.cs
namespace OdeToFood.Models
{
public class Restaurant
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Country { get; set; }
public ICollection<RestaurantReview> Reviews { get; set; }
}
}
OdeToFood.cs
namespace OdeToFood.Models
{
public class OdeToFoodDb : DbContext
{
public DbSet<Restaurant> Restaurants { get; set; }
public DbSet<RestaurantReview> Reviews { get; set; }
}
}
Controller
OdeToFoodDb _db = new OdeToFoodDb();
public ActionResult Index()
{
var model = _db.Restaurants.ToList();
return View();
}
Index.cshtml
#model IEnumerable<OdeToFood.Models.Restaurant>
#{
ViewBag.Title = "Home Page";
}
#{
if (Model != null)
{
foreach (var item in Model)
{
<div>
<h4>#item.Name</h4>
<div>#item.City, #item.Country</div>
<hr />
</div>
}
}
else
{
<h1>Null</h1>
}
}
You need to pass to model back to the view.
OdeToFoodDb _db = new OdeToFoodDb();
public ActionResult Index()
{
var model = _db.Restaurants.ToList();
return View(model);
}
You never actually send the model to the view. Pass it as an argument:
OdeToFoodDb _db = new OdeToFoodDb();
public ActionResult Index()
{
var model = _db.Restaurants.ToList();
return View(model);
}
Additionally, it's generally a good idea not to create database contexts in a shared scope. Keep the context as close to where it's used as possible and only expand its scope when you really need to. Something like this:
public ActionResult Index()
{
using (var _db = new OdeToFoodDb())
{
var model = _db.Restaurants.ToList();
return View(model);
}
}
Database contexts/connections in a shared scope is just asking for problems unless you pay close attention to what you're doing. As the code gets more complex, it becomes more likely that other methods will try to use it and it may be in an unknown state at that time.
I'm new to MVC, so I apologize in advance if something doesn't make sense.
I have a base class (let's say "Person"), and 2 derived classes ("Student", "Professor").
I want to use 1 view for the Create functionality, with Partial views that contain the creation forms for either a student or professor. If I add a parameter, I can check against that to determine which partial view to show.
But my question is this: When the "Create" button is clicked, how can I determine which object is being created?
Edit (please bear w/ me, as I just created these to illustrate the problem)
Person class:
public class Person
{
public string Gender { get; set; }
public int ID { get; set; }
}
Student class:
public class Student : Person
{
public string LastName { get; set; }
public string FirstName { get; set; }
public List<Course> Courses { get; set; }
}
Professor class:
public class Professor : Person
{
public string LastName { get; set; }
public string FirstName { get; set; }
public double AnnualSalary { get; set; }
}
So then my Create controller looks like this:
public ActionResult Create(int personType) //1=student, 2=professor
{
var x = new {
Student = new Student(),
Professor = new Professor()
};
ViewBag.PersonType = personType;
return View(x);
}
Then my view looks like this:
<div>
#if (ViewBag.PersonType == 1)
{
#Html.Partial("CreateStudentPartialView", Model.Student)
}
else
{
#Html.Partial("CreateProfessorPartialView", Model.Professor)
}
So, the question is what would the associated create action look like, when the "Create" button is clicked in either partial view?
[HttpPost()]
public ActionResult Create(....) //What would I put as parameter(s)?
{
//no idea what to do here, since I don't know what object is being passed in
return RedirectToAction("Index");
}
Your best bet here is to have multiple POST actions in your controller.
So in the forms in your partial views, specify the action to hit
#using (Html.BeginForm("CreateStudent", "Create")) {
and
#using (Html.BeginForm("CreateProfessor", "Create")) {
Then your controller will look something like this:
[HttpPost]
public ActionResult CreateStudent(Student student)
{
//access the properties with the dot operator on the student object
//process the data
return RedirectToAction("Index");
}
and
[HttpPost]
public ActionResult CreateProfessor(Professor professor)
{
//access the properties with the dot operator on the professor object
//process the data
return RedirectToAction("Index");
}
I'm a newbie to asp.net mvc and I'd like to know if what I do is correct.
I want to create a view for searching people. Here's what I have so far:
The business model class:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
public DateTime DOB { get; set; }
public List<Telephone> Telephones { get; set; }
}
The ViewModel class:
public class SearchPersonViewModel
{
public int Id { get; set; }
public string FullName { get; set; }
public string LicencePlate { get; set; }
public string CarMake { get; set; }
public string CarModel { get; set; }
}
The partial view :
#model IEnumerable<MvcApplication2.Models.SearchPersonViewModel>
#foreach (var item in Model)
{
#Html.DisplayFor(m => item.Id)
#Html.DisplayFor(m => item.FullName)
}
The view from which the partial view is called:
#Html.Action("Search", "Person");
*The controller method in the PersonController:
[ChildActionOnly]
public ActionResult Search()
{
List<SearchPersonViewModel> model = new List<SearchPersonViewModel>();
model.Add(new SearchPersonViewModel() { FullName = "test", Id = 3 });
return PartialView("_SearchPerson", model);
}
Now the problem is that the Search method is called whenever the main View is loaded. What I want is to add a search textbox on the mainview for filtering the collection rendered in the partial view. How could I do that ?
This way your partial will render on click only
<script>
(function($){
$('#btn').click(function(){
$('#searchresult').load('#Url.Content("~/Person/Search")');
}
</script>
Make an ajax request to /search and append the data to your page.
<div id = 'searchresult'>
#Html.Action("Search", "Person");
</div>
whenever your want to filter call something like $('#searchresult').load('/search?q=xxx');
You could use 2 ways:
microsoft ajax helpers
jquery
For both of them you need to remove [ChildActionOnly] and #Html.Action("Search", "Person");
Look at this example: Using Ajax.BeginForm with ASP.NET MVC 3 Razor
I am having a twitter bootstraper tab, with 3 simples tabs (Caracteristiques, Certificat and Contrats)
the tabs are ajax load with asp.net mvc partialviews, they are tied with modelViews:
the partials views strongly tied to the type ViewModel:
// Load Certificat
public ActionResult Certificat()
{
var modelStaffs = _twitterTabsModel.GetStaffs();
return PartialView("_Certificat", modelStaffs);
}
// load Contrats
public ActionResult Contrats()
{
var modelJoueur = _twitterTabsModel.GetFirstJoueur();
return PartialView("_Contrats", modelJoueur );
}
the models:
public class TwitterTabModel
{
public ModelJoueur JoueurVM { get; set; }
public IEnumerable<ModelStaff> StaffVM { get; set; }
}
public class ModelStaff
{
public string NomStaff { get; set; }
public string FonctionStaff { get; set; }
}
public class ModelJoueur
{
public string NomJoueur { get; set; }
public string PrenomJoueur { get; set; }
}
the Caracteristiques Tab views:
#model Controls.Models.ViewModel.TwitterTabModel
<h2>Caracteristiques</h2>
#using (Html.BeginForm())
{
.... the tabs code ...
<p>
<input type="submit" value="Save" />
</p>
}
The tabs load fines, what I want to do is to include a submit button on the first razor view tab, it submit all the other models if loaded, however, when I get the post call, all the others models, JoueurVM and StaffVM are empty even though they are loaded. Why is it according to you ?
edit: This is the controller post code, nothing special, just trying to get the twitterTabModel:
[HttpPost]
public ActionResult Tabs(TwitterTabModel model)
{
return View();
}
Thanks
I figure a workaround, I pass the input values back as formcollection, instead of the overall model, well it it' s not clean, but well, it works as i can get all the values posted