MVC :: Set Model For Controller?
Jan 20, 2010How to set the model for the particular controller from another controller ?
View 1 RepliesHow to set the model for the particular controller from another controller ?
View 1 Repliesi have vreater a class model for login
public class LogOnModel
{
[Required(ErrorMessage="*")]
public string username { get; set; }
[Required(ErrorMessage = "*")]
[DataType(DataType.Password)]
public string password { get; set; }
public bool rememberme { get; set; }
}
and created a view from controller
public ActionResult LogOn()
{
return View();
}
[HttpPost]
public ActionResult LogOn(string Username, string Password, bool RememberMe)
{
if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
return RedirectToAction("Index", "Home");
else
return View();
}
my view
<% using (Html.BeginForm())
{ %>
<div>
<label>username</label>
<%: Html.TextBoxFor(m=>m.username) %>
</div>
<div>
<label>password</label>
<%: Html.TextBoxFor(m=>m.password) %>
</div>
<div>
<label>Beni Hatırla</label>
<%: Html.CheckBoxFor(m=>m.rememberme) %>
</div>
<div>
<label></label>
<input type="submit" id="button" value="Login" />
</div>
<div id="LoginError">
<%: Html.ValidationMessageFor(m => m.username)%>
<%: Html.ValidationMessageFor(m => m.password) %>
</div>
<%} %>
but password textbox type does not include password charecters as(***). and not Html.ValidationMessage for empty textboxes.
i need to implement edit functionality for my model. among other fields my model contains record id in the database. when my controller is called i need to update the database record with the same id as the model using the fields in the model.
here is my controller:
[HttpPost]
public ActionResult Edit(MyModel m)
{
var r = my_datacontext.MyTable.SelectSingleOrDefault(x => x.id == m.id);
if (r != null)
{
r = m;
my_datacontext.SubmitChanges();
}
}
this does not work because m does not get added to the datacontext's ChangeSet. why? i fixed this by changing the signature of my controller action to receive FormCollection instead of the model, and by invoking UpdateModel manually. i think this is not the right way to do it.
In MVCs with which I have worked, the Controller has the job of co-ordinating a number of views, consequent to some user action against the model.
However in ASP MVC, there never appears to be more than 1 view resulting from an http request (please correct me if I am wrong). Instead the "Controller" in ASP MVC appears to be a URL Routing Target.
Also, in my (admittedly limited) experience, the Model in MVC is intended to be a model of the problem domain of the application. However in ASP MVC, the "Model" appears to be a model of the
data binding of the corresponding view.
Why is a null parameter being passed to the following controller action?
public FileContentResult GetImageForArticle(ArticleSummary article)
{
if (article == null || !article.ContainsValidThumbNail()) return null;
return File(article.ThumbNail, article.ThumbNaiType);
}
from the following partial view:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<AkwiMemorial.Models.ArticleSummary>>" %>
<%if (Model.Count() > 0) [code]...
Has anybody figured out a way to separate the dependency of the view/controller from the model. In other words, model is independent of the view/controller in asp.net MVC but has anybody also taken out dependency of the view/controller from model as well?
View 4 RepliesI've been going at this for several hours and I simply cannot find the solution.
I want to get some data from my user. So first, I use a controller to create a view which receives a Model:
[code]...
The rest of this controller does not matter since no matter what I do, the count attribute of Request.Files (or Request.Files.Keys) remains 0. I simply can't find a way to pass the files from the form (the Model passes just fine).
returning a partial view from a controller with different model than my main View. For example:
blic ActionResult Index()
{
//myModel - get Some Types
return View(mymodel);
}
[code]...
I am using the Nhibernate with MVC. In the controller i am getting the model from the HttpSession. How can i set the model from controller Unit testing method.
View 1 RepliesHow to pass model bind collection from one controller to another?
I have this model class,
Public Class Sports
Private _SportsID As Integer
Private _SportsName As String
[Code]....
I'm having trouble connecting to my model to my existing database using Entity Framework . Here is the code I'm working with:
[Code]....
How Can I Pass data(not the part of model) to Controller from View? View Code
[Code]....
Currently, I am working with ASP.NET MVC1 and am still learning about Model Binding and how values from a View are passed back to the Controller / Model. Specifically, I want take an existing Model, create a Table and populate the Rows of the Table, allow the user to edit some fields and pass it back. In my example, I have a Class called "Ingredient" which has 4 public accessories: Name, Barcode, Amount, and Unit.
[Code]....
Or is this not possible? (Basically, I'm trying to re-create a datagrid where certain fields are editable and certain are not...)
Curious what the best practice is for returning errors to a view from a controller where the error isn't really a validation error, but more like "user not found" or "service timeout" type of errors. (in ASP.NET MVC2 framework)
I've been adding them to the ModelState's model errors, but that doesn't seem appropriate. (although easy to implement and maintain)
Example - A user tries to log in, and their credentials do not match a known user.
When I first heard about ASP.NET MVC, I was thinking that would mean applications with three parts: model, view, and controller.
Then I read NerdDinner and learned the ways of repositories and view-models. Next, I read this tutorial and soon became sold on the virtues of a service layer. Finally, I read the Fluent Validation documentation, and I'll be darned if I didn't end up writing a bunch of validators.
Tonight, I took a step back and thought about what had become of my project. It seems to have become the victim of the design pattern equivalent of "feature creep". Somehow I'd gone from Model-View-Controller to Model-Repository-Service-Validator-View-ViewModel-Controller. You want loosely coupled and DRY? We got your loosely coupled and DRY right here! But I'm wondering if this could be a case of too much of a good thing.
Am I right to be concerned? Or is this actually not as crazy as it sounds? On one hand, it seems crazy to have so many layers. On the other hand, every layer has a clearly defined purpose that makes sense to me. Have your MVC applications turned into MRSVVVMC apps too? If not, what do they look like? Where's that right balance?
Basically I have RegisterModel2 that has a Person class. My requirement is that Phone Number and Address are not required for registration. But if they try to enter a Phone number of Address then it should validate it. Problem is that it is always stating that the child fields of Phone (area code, number) and child fields of Address (street1,city,etc) are required. Is there a way to annotate in the RegisterModel that a parent class is not Required but if any data in a child element is given then and only
then should the child elements be validated?
My Person EDM class has a nullable 0.1 -> 0.1 navigation property to Phone and Address.Here is the person class
[MetadataType(typeof(Person_Validation))]
public partial class Person
{
}
public class Person_Validation [code]....
The Person EntityObject has an optional/nullable Navigation Property to the Phone Object.
The Phone object has these annotations,[Code]....
I have RegisterModel defined as follows:
[Code]....
My View looks like this:
[Code]....
And here is my Controller Action it is posting to:
[Code]....
getting data from multiple Entities models into an MVC controller. Most of the examples I have seen for using EF in an MVC2 app only use a single entitiy.
I have just started using MVC2 in C# using the Entity Framework models (CIOps.model) created from an SQL database. I can create the controller and views using single tables of the model in MVC2, but I just cannot get my head around how to get data from multiple entity tables into the controller (similar to joins in T-SQL). I have included an example below of the controller code that works with a single entity table, tbl_tours (tbl_tour in DB).
Could someone please illustrate how this code would be changed to include additional columns from FK tables in addition to tbl_tours columns. E.g. the clients name from the tbl_clients, the coordinators name from the tbl_employees, and costs from tbl_costs entities? Is it possible to do this directly using the EF model entities/classes that are already created and not use LINQ, POCO, Repositories, etc.? The FK relationships are already in the EF models. I have included the whole controller code, but I just need a few examples of how to join the multiple entities, not rewrite every CRUD function function in the controller. I think this will get me over the hump in using EF in an MVC2 App. Also ignore the fact I am using the home controller, this will change in the application.
[Code]....
I'm displaying the values in ViewData.Model in a View (.aspx) -- After the user looks them over, when the user clicks the "Submit" button that I have in this view, I want the ViewData.Model passed to another controller, but I'm not sure how to do this.
Currently, my target controller/action looks like this:
[Code]....
The "ViewData.Model" variable in this View is of type: List<Sample>
How do I pass the ViewData.Model to the RegisterController / Samples (action) ?
I've got problem with my app .
I've got such classes (this is some kind of tree structure):
[Code]....
[Code]....
in Index() action i've got this piece of code
[Code]....
[Code]....
UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development, that follows the UP(Unified Process). It uses a Grasp Controller pattern to interact with domain classes by some methods like NewSale(), AddNewItemToSale() and CloseSale. In windows form, I can instantiate a object of this class in the UI and then use its methods to perform the actions. This works well in Client apps, but when I use asp.net mvc, I cannot find a way to instantiate an object (one for each user) that was always visible for a Controller (MVC). I cannot insert as an attribute inside Controller because it always create a new one.
View 1 Replies my MVC2 delete and only my delete controller is refusing to return any class information. Its really similar to my edit function and the views are all auto-generated so I don't see the problem.
Function Delete(ByVal id As Integer) As ActionResult
I have a simple model where a Person has Gifts. I have a view which is a list of Gifts belonging to one Person.
My problem is with the Create action for a new Gift. I want it to default to the PersonID that we are already viewing the list of Gifts for. I tried simply passing the last PersonID (they are all the same)
Html.ActionLink("Create New", "Create", new { id = Model.Last().PersonID }) which works fine if there is already at least one Gift for that person but if this is the first Gift I don't have a value.
My Gift List controller knows the PersonID I want to pass but the view doesn't.
How do I pass this PersonID from my Gift List controller to my Gift Create controller via the Gift List view? Or is there a better way to do this?
I have a filter on my MVC web site. I display some records in a few different controller actions but when moving from one action to another I want to apply those filter values.
How can I persist values from controller to controller?
Should I use Session? TempData?
I am using Structure Map for IOC.
Maybe I could have a class that contains a Property for each Session Value that I use in my application and inject it on the controllers that need session?
I'm building an MVC 2 RTM app, and I want to be able to share my model across applications. I'd *like* to be able to implement it like:ASP.NET MVC2 app (holds Views and Controllers)Class library to hold Model(s)WCF app to handle the data transactions with the models via different data stores across apps I had the MVC app working fine, but I wanted to abstract the data stuff and be able to work with the model across apps through the WCF site, so I created a class library project and moved all of the Models classes into that and set-up a WCF app, then added project references to the MVC and WCF apps that point at the class library. The idea was I can create services that take and return objects from the model via method calls across apps. It appears that everything's wired up correctly in the MVC project, so I'm passing the objects stored in the Models class library between controllers and views and everythig is compiling just fine, but for some reason the data is not being passed back from the views to the controller on POST -- all of the properties in the classes are null or empty.
When I debug the app, I can see that the values are stored in the model data dictionary but not the model object itself. What am I doing wrong? Am I on the wrong path, or missing something obvious (to some)?
Is it possible, inside a Custom Model Binder, to fire "something" that "says" the value is invalid so it gets handled by validation part?
Basically, I am getting an exception when the value for the property is invalid.