MVC :: Can Pass The _businessLayer Variable From The Controller To The Viewmodel?
Dec 21, 2010
I am creating an instance of a business layer in the constructor of my Controller and then I am passing this to my view model like this;
[Code]....
The problem is when I make a post from the view with the viewmodel, I get an error that MyViewModel doesn't have a parameterless constructor defined, and I don't want it to have. Is there anyway I can pass the _businessLayer variable from the controller to the viewmodel when it's constructed "on the fly" in a method like this;
[Code]....
Maybe by using a custom model binder?
View 15 Replies
Similar Messages:
Mar 17, 2011
I have this controller:
public ActionResult Details(String UserName)
{
using (var db = new MatchGamingEntities())
{
var Users = from m in db.Users
join m2 in db.MyProfiles on m.UserId equals m2.UserId
where m.UserName == UserName
select new UserViewModel
{
UserName = m.UserName,
LastActivityDate = m.LastActivityDate,
Address = m2.Address,
City = m2.City,
State = m2.State,
Zip = m2.Zip
};
return View(Users.SingleOrDefault());
}
}
I type in the url Profiles/Details/hello, but it does not work, if I do Profiles/Details?UserName=hello then it works. I have another ActionResult just like this except taking in an int id as parameter and it works fine with the first url format.
View 1 Replies
Jul 1, 2010
I am currently designing a new .NET application and would like to keep it UI independant.
Whilst I initally would like to use WPF, I would like to have the option of swapping the UI to ASP or WinForms if necessary.
In a layered design:
View - Interface Controller (ViewModel) - Model - Persistance
is it possible to design the Interface Controller so that it will work with different view technologies, or will I need to replace the interface controller at the same time as the View?
View 2 Replies
Dec 13, 2010
I am trying to pass a view model of mine, which has multiple list in it, to my view. Then in my view i need to edit the different list. Then on my post i need to save the edits. Although, when i pass my viewmodel back to my post, it is empty! Can somebody explain what i am doing wrong? Controller
[Code]....
here is my View
[Code]....
NewsViewModel
[Code]....
View 2 Replies
Jan 19, 2010
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?
View 2 Replies
Sep 5, 2010
In my page there is a Login form. It looks like
[Code]....
But at the same time in this page I need to render some info from another ViewModel. Is it possible to write in action something like
return View(Model1, Model2); ?And how could I get access to these ViewModels in this case?
View 2 Replies
Mar 23, 2011
I have the following scenario;
<%
using (Html.BeginForm("actionname",
"ControllerName", new
[code]...
View 1 Replies
Mar 23, 2011
I'm trying to do a post with jQuery in an MVC application, according to this SO answer here:Can jQuery do a POST of a ViewModel to a Controller in ASP.NET MVC?The difference is I am using it to delete an item in a dynamic view (never mind the fact that I'm posting a delete directly, this is a closed site by authorization, and I will use jQuery to confirm, I just don't want the user to have to go to a new page). And I therefore need to be able to send both the id and the ViewModel (the ViewModel to save any added items before deleting any). I'm not particularly happy with the solution by and large, but at this point I just need to get it to work!So I tried to figure out how to send both the id and the ViewModel according to the SO post above, but I can't figure out how to get the ViewModel in with the named parameters. This doesn't work:
$(".delete").click(function () {
$.ajax({
type: "POST",
[code]...
View 3 Replies
Aug 16, 2010
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?
View 2 Replies
Jun 11, 2010
when we need to pass multiple data items using a view model from a controller to a view, for example, to pass both a collection of products{id, brand, price, name} and collection of product categories{id,type} to a view, how can i validate both of these two entities?
if there is only one entity "products in the model", the validation should be somewhat like this
[Code]....
View 2 Replies
Feb 26, 2011
how to page index.cshtml call variable from controller
i using vs2010 & mvc3.0
View 2 Replies
Aug 25, 2010
I have a drop down where I want to pass the selected value I selected to the controller. How do I accomplish this?
View 2 Replies
Nov 15, 2010
I have an action:
public ActionResult Details(int? aaaID, int? bbbID)
and I am trying to route the url http://..../controller/aaa/23432 to action Details.
routes.MapRoute( "controller", // Route name "controller/aaa/{id}", // URL with parameters new { controller = "controller", action = "Details", lblID = UrlParameter.Optional } // Parameter defaults);However, the aaaID of function Details also get a null value?
View 1 Replies
Mar 15, 2011
[code]....
i am having problem passing in passing the url id to the controller with the form . how can i do that ?
View 3 Replies
Oct 4, 2010
In my ASP MVC app I have an asp: DropDownList control that has a source of a Linq data class. The values in the list are getting set fine, but when I select one, and then click on an Html.ActionLink, and pass control back to my controller, I'm not sure how to fetch the value that was selected from the DropDownList.
View 1 Replies
Jul 29, 2010
How Can I Pass SelectList into Controller?
here is my code
[Code]....
View 1 Replies
Feb 16, 2011
I am trying to get this functionality on my jokes website:
www.mysite.com/category/bar
To get all the bar jokes ..etc
The way I have my route right now is:
{controller}/{action}/{id}
which means I have to type:
www.mysite.com/category/index/bar
the problem is that even with that, the string parameter "bar" isn't being acknowledged ! ( I know from debugging). Here is my Index action in the Category controller:
[Code]....
View 8 Replies
Mar 6, 2011
I have some textboxes and a table of data created client side that I want pass to a controller as JSON.
I want to use Jquery to enumerate the table.
Assume I have 2 textboxes called name and age. Assume a table with 2 columns. one column with class called phonetype and one column class called phonenumber.
So how do construct the JSON from this?
View 1 Replies
Apr 18, 2010
i have a view /categoria/index
[Code]....
i am trying to call the action from /imovel/index
with an id
but the thing is that i am getting null
[Code]....
View 2 Replies
Sep 23, 2010
Iam trying to pass data from my controller, and put it into the variable in JS script. I have done it before and it worked perfectly, thats why this one puzzles me even more.
Here is my code of the controller:
[Code]....
In chrome I also get an error "Uncaught SyntaxError: Unexpected token ILLEGAL". I am pretty sure it is the same I use for my other project and that one works just great so I really dont know what is wrong.
View 3 Replies
Jan 14, 2010
I've a controller with different actions that calls the DB code and return result. I want to pass the value of text box to different actions in controller.
How to do it? I know that, I can pass values by using form. But, I don't to know how to call different actions in controller from single view.
View 8 Replies
Jan 13, 2011
This may seem simple to a seasoned coder, but I'm all wet-eared and new to MVC, and trying to wrap my head around it is driving me nuts. I have completed the Nerd Dinner and MVC Music Store tutorials, and seem to be grasping the basics... Well, sort of... It just seems that every new idea I have requires hours of searching with Google.
As it stands at the moment, I have a static DateTime field that is populated by using DateTime.Now. What I need to implement is a master TextBox (JQueryUI DatePicker) at the top of the View that will enable the user to overwrite said DateTime.Now if required.
Here is a hypothetical situation:
Controller
[Code]....
Site.Master
[Code]....
View
[Code]....
I thought of using a variable to override the default date if the user selects a master date, but can't figure out how to implement it.
I can get it to work if I use a static string, like so:
[Code]....
Is there any way to pass the value of the master datepicker (txtDateTime) from the View back to the Controller? I've heard mention of TempData, but can't seem to find a similar scenario.
I'm sure I'm just missing something really obvious, so if anyone could explain it to me really s-l-o-w-l-y, that would be cool.
I'm praying that this isn't another of those situations where "MVC doesn't work that way" or "It needs more cowbell", 'cause then I'll pack it all in and go play WoW D:
View 8 Replies
Oct 8, 2010
How to pass the textbox value to my controller there is no form and post method in the viewpage.I have taken one input type textbox which is not bind with my model.I need to access that value to controller
View 13 Replies
Feb 9, 2010
Consider the following data
<xml>
<id>10</id>
<value1>15<value1>
</xml>
I need to use this data as an input to ajax request. For eg. consider a dropdownlist change event, I need to pass these values to the controller.
sample code
//dropdown change event
$("#TestReqID").change(function() {
var id = 10;
var testResults = "abc";
var inXML = "<xml>";
inXML = inXML + "<id>" + id + "</id>";
inXML = inXML + "<value1>" + testResults + "</value1>";
inXML = inXML + "</xml>"
$.getJSON("/Home/UpdateTest/Json/" + inXML, function(data) {
//success code goes here
});
});
in HomeController
public ActionResult UpdateTest(string obj)
{
if (HttpContext.Request.IsAjaxRequest())
{
/*
Need to parse data in "obj" from ajax request
*/
IPTests test = _service.GetIPTest(Convert.ToInt32(id));
return new JsonResult { Data = new { ipTestId = "0", testResults = "" } };
}
return View(test);
}
instead of xml string is it possible to use javascript object like
[Code]....
View 2 Replies
Feb 13, 2011
I want to pass a message from the controller to a view using Viewdata. Here is my code:
public ActionResult Create(FormCollection createPage)
{
try
{
......................
ViewData["Message"] = "Success - rec added!!!!" ;
return RedirectToAction("Index");
}
catch (Exception e)
{
ViewData["Message"] = "Exception: " + e.ToString();
return RedirectToAction("Index");
}
On my View I have:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>::><%: ViewData["Message"] %><::</h2>
<h3>==><%= ViewData["Message"] %><==</h3>
<p>
This is Index.aspx page in Controller folder
</p>
</asp:Content>
I am getting result with empty ViewData["Message"]
View 4 Replies