MVC :: Model With Parameter In The Constructor?

May 14, 2010

I have the following constrcutors within a Model and I want to click the create button on a previous view and pass the projectID in and call the second Constructor so I have the projectID on this view. Do I need to change the routing in someway to call the constructor with the parameter? -

[Code]....

View 2 Replies


Similar Messages:

MVC :: View Model Class With Parameters In Constructor

Jan 4, 2010

I just struck upon a prime opportunity to further exhibit my MVC ignorance, by way of the following: How can one make use of a custom view model class that has parameters in the constructor? For example, a class that could intitialize various collections/properties based on an IPrincipal object passed in. I've come across several allusions to this scenario, involving a class inheriting DefaultModelBinder which overrides the CreateModel method, but have yet to find an example.

[Code]....

View 3 Replies

Microsoft Unity. Specify A Certain Parameter In Constructor?

Oct 30, 2010

I'm using Microsoft Unity. I have an interface ICustomerService and its implementation CustomerService. I can register them for the Unity container using the following code:

container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager());

If CustomerService has a certain parameter in its constructor (e.g. ISomeService1), I use the following code (I need to specify SomeService1):

container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager(), new InjectionConstructor(new SomeService1()));

No problems here. The problem appears when CustomerService class has two parameters (not one param as in the previous example) in its constructor (e.g. ISomeService1 and ISomeService2). It works fine when I'm using the following code: container.RegisterType(new TransientLifetimeManager(), new InjectionConstructor(new SomeService1(), new SomeService2()));The problem is that I do not want specify SomeService2() for the second parameter. I want to specify only first parameter - SomeService1(). But I get the error that I need to specify none or both of parameters. How can I specify only first parameter of constructor?

View 3 Replies

MVC :: Edit Validation In 2 - Error "No Parameter Less Constructor Defined For This Object"

Jan 10, 2010

i am taking data from two tables so i created one class and i constructed with two tables like this..

[Code]....

my edit post method is:

[Code]....

if i put PublisherViewModel in post function edit (ByVal aintEntityId As Integer, ByVal model As PublisherViewModel) As ActionResult. i am getting this error when post edit function called. No parameterless constructor defined for this object.

View 2 Replies

Web Forms :: Usercontrol Constructor Parameter - Unable To Pass The Value To Usercontrol?

Apr 6, 2010

im creating site with usercontrols. I have repeater, inside it is another repeater with usercontrol. My problem is passing BindedValue to usercontrol.Bindings works fine and binds the value i need but it cannot pass the value to usercontrol. When i type it manually it works, when i bind it, it passes null (!).I've tried get,set, functions (ondatabound, onload, oninit), accessing control from code with no luck. Ive read and tried to do all the google solutions but with no luck. (even with creating usercontrol inheritance)

View 5 Replies

C# - MVC - Binding Of Parameter To Model Value?

Jun 14, 2010

This seems like the model binding is causing me issues.Essentially I have a model called ProductOption and for the purpose of this question it has 2 fields

ID (Int) PK
ProductID (Int) FK

I have a standard route set-up

context.MapRoute(
"Product_default",
"Product/{controller}/{action}/{id}",

[code]...

This is used to determine (on submit) if we are editing or adding a new option. However the Model binder in .net seems to replace .ID (Which was 0 when leaving the above get actionresult) with 1 (or the value of the id parameter in the URL)How can I stop or work around this?

ViewModel
public class ProductExtraModel
{
//Database
public int ID { get; set; }
public string Name { get; set; }
public int ProductID { get; set; }
public ProductModel Product { get; set; }
}

View 2 Replies

MVC :: Post Action Model Parameter

Jan 26, 2011

i wonder something about mvc post method. for example I created a view "strongly typed" from a model (MyModel).

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Demo1.Models.MyModel>" %>
for Create actionsthere is two way.
first one takes MyModel as parameter
[HttpPost]
public ActionResult cart(MyModel md, int someID)
{
//some code
//.....
return View(MyModel);
}
second one does not take parameter as model.
[HttpPost]
public ActionResult cart(int someID)
{
//some code
//.....
return View(Mymodel);
}

for first one way, after form post the form values does not delete, I mean form does not cleared,after second one form is be cleared. why this diffirence?

View 5 Replies

MVC :: Bug - Get Value From Route Parameter Instead Of Property Of Model

May 18, 2010

I found little bug feature in MVC 2.

So, if we have in route defined parameter with same name as property at model and then we call it from strongly typed HTML helper (for example Html.TexBoxFor(x => x.PropertyName)), we get value from route parameter instead of property of model.

For example.

We have defined follow route:

[Code]....

Very simple model:

[Code]....

Next action in controller:

[Code]....

And very simple view:

[Code]....

Result you can see at follow picture:

I think what this dirty example described the essence of the problem. Let me know if I'm wrong.

View 1 Replies

What's Another Name Of Constructor

May 10, 2010

What is another name of Constructor?

View 3 Replies

ADO.NET :: The Constructor Of ObjectContext

Mar 8, 2011

I am confused about the constructor of ObjectContext

// Summary:
// Initializes a new instance of the System.Data.Objects.ObjectContext class
// with a given connection string and entity container name.
//
// Parameters:
// connectionString:
// The connection string, which also provides access to the metadata information.
//
// defaultContainerName:
// The name of the default entity container. When the defaultContainerName is
// set through this method, the property becomes read-only.
protected ObjectContext(string connectionString, string defaultContainerName);

I am not understanding the parameters clearly.

View 3 Replies

C# - Why Need A Private Constructor

Apr 6, 2010

if a class has a private constructor then it cant be instantiated. so, if i dont want my class to be instantiated and still use it, then i can make it static. what is the use of a private constructor? Also its used in singleton class, except that, is there any othe use ? (Note : The reason i am excuding the singleton case above is because I dont understand why do we need a singleton at all ? when there is a static class availble.

View 8 Replies

MVC :: Store Model In Separate Class Library- Pass Model Objects?

May 19, 2010

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)?

View 2 Replies

C# - Why DataClassesDataContext Has No Default Constructor

May 25, 2010

I have a connection string defined in my web.config like:

<connectionStrings>
<add name="LibraryConnectionString" connectionString="Server=.SQLEXPRESS3;Database=Library;Integrated Security=true" />

Well...lI do not understand why when i drag-and-drop tables into a new DataClasses dbml it does not construct a default constructor specifying the connection string. I only have constructor with params like:

public DataClassesDataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}

I do need a default constructor for LinqDataSource..

View 1 Replies

MVC :: Access Model Validation Inside Custom Model Binder?

Sep 1, 2010

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.

View 1 Replies

ADO.NET :: Entity Model - Update Model From DB And The Assembly Reference Seems To Be Missing?

Jan 10, 2011

I made a few changes to the DB in SQL server management studio then right clicked on the .edmx doc to get it to update. That seemed to work fine but when i compiled the app everything that referenced the EF seems to be broken.The Error list now contains the below error for all classes that used it.

The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?)

View 4 Replies

HttpHandlers / Modules :: Process Model Or Thread Model Of A Website?

Mar 11, 2010

I have an issue of static variable in an asp.net application. Let's say I have a server with 8 CPUs running windows 2008 R2 hosting an asp.net webbsite, nothing fancy here, no funny configuration.

Is the following statement true? at any single time, there's one and only one process is running and accepting requests, even in the recycling stage. The reason I am asking is: I have a static field in my class, and I want to make that's the only static instance in the website. I've heard in some circumstances, IIS is recylcing your application, and it will start a new process, but the old process is still working, thus I will have 2 static instances in memory, which defeat the purpose of static field.

One step further, let's assume there might be 2 instances in memory, can I assume there are at most 2 instances at any single time? And can I assume once the second instance is up, the first instance will NEVER accept new requests?

Another question: Recently I have a problem with an applicationdomain concept. Looks like if an application domain causes a memory leak, unload the domain will not release the memory (Umanaged leak). So to what extent Application Domain is isolated?

View 1 Replies

Web Service Error ' It Does Not Have A Parameterless Constructor '?

Aug 7, 2010

I've created a web service , which can a method to set the user credential using Microsoft.Web.Services3.WebServicesClientProtocol. The sample code is :

<WebMethod()> _
Public Sub ClientCredential1(Of TSecurityToken As SecurityToken)_
(ByVal UserCred As Microsoft.Web.Services3.Security.Tokens.UsernameToken)

[code]....

When I run the web service it gives this error: "Microsoft.Web.Services3.Security.Tokens.UsernameToken cannot be serialized because it does not have a parameterless constructor."

View 3 Replies

C# - Availability Of Methods On The Basis Of Constructor?

Jan 19, 2011

I have a Class called Repository for accessing (Read/Write to and from) Database.The projects that require access to the database create an object of Repository and pass the connection string as a constructor parameter for the repository to work for that particular project.
I have few methods in the Repository that i want to be available only if certain connection strings are passed. I don't want them to be available if some different connection string is passed.Is there any way I can accomplish that?I have never used method header technique, if yes, how can i use it? if no, please let me know if there is any other way to achieve my goal.

View 6 Replies

Default Model Binder Does Not Bind Model Class

Dec 28, 2010

I am trying to make a post that should use the Default Model Binder functionality in ASP.NET MVC 2 but unfortunately I can't get through. When I click on the checkout button I populate a form dinamically using jQuery code and then submit this form to the server. This is the form that get submitted

<form action="/x/Order/Checkout" id="cartForm" method="post">
<input name="__RequestVerificationToken" type="hidden" value="UDjN9RdWheKyWK5Q71MvXAbbDNel6buJd5Pamp/jx39InuyYIQVptcEubIA2W8DMUzWwnZjSGkLspkmDPbsIxy8EVuLvfCSZJJnl/NrooreouptwM/PaBEz2v6ZjO3I26IKRGZPqLxGGfITYqlf8Ow==">
<input id="CustomerID" name="CustomerID" type="hidden" value="1">
<input id="FirmID" name="FirmID" type="hidden" value="2">
<input type="hidden" name="CartItems[0].ServiceTypeID" value="1">
<input type="hidden" name="CartItems[0].Quantity" value="1">
<input type="hidden" name="CartItems[1].ServiceTypeID" value="2">
<input type="hidden" name="CartItems[1].Quantity" value="1">
</form>

This is the jQuery code that handle the submit event for the form
$("#cartForm").submit(function (event) {
event.preventDefault(); var form = $("#cartForm");
var panel = form.parent(); panel.parent().block();
$.ajax({ type: "post", dataType: "html",
url: '<%: Url.Content("~/Order/Checkout") %>',
async: false, data: form.serialize(),
success: function (response, status, xml) { panel.parent().unblock(); },
error: function (response) { panel.parent().unblock(); } }); });

This is the controller action that should be get called
[HttpPost]
[ValidateAntiForgeryToken]
public virtual ActionResult Checkout( CartModel cart ) {
} And finally this is the CartModel class involved
public class CartModel : BaseModel{
public int CustomerID { get; set; }
public int FirmID { get; set; }
public List<CartItemModel> CartItems { get; set; }
public CartModel() { CartItems = new List<CartItemModel>();
} } public class CartItemModel : BaseModel
{ public int ServiceTypeID { get; set; }
public int Quantity { get; set; } }

But the default Model Binder does not bind the web form data to a CartModel class. Using Fiddler I have been able to see that the data sent to the server is correct as you can see from the following snapshot.

View 1 Replies

No Parameterless Constructor Defined For Object Error - How To Fix It

Apr 22, 2010

I'm very new to Visual Studio and I am getting this error when trying to update an Oracle table from Gridview using a tableadapter datasource. I've followed the instructions in my text very closely but am at a loss as to why this error is occurring.

Details: System.MissingMethodException: No parameterless constructor defined for this object.

Source Error:

[Code]....

Stack Trace:

[Code]....

this is the stack trace error I am getting. At this point, I'm a little clueless how to resolve it.

View 2 Replies

MVC :: Getting Error - No Parameterless Constructor Defined For This Object

Sep 10, 2010

I am doing Music Store sample application.Here my code.

StoreViewModel

[Code]....

StoreController

[Code]....

Create.aspx

[Code]....

But when i click Save it show Error

Server Error in '/' Application.

No parameterless constructor defined for this object.

View 3 Replies

Call The Static Constructor After Creating The Object?

Oct 27, 2010

when we declared as static then we will not create the object to call the static method. then my doubt is how is call the static constructor after creating the object?can you explain cleary?

View 1 Replies

Handling Exceptions That Happen In A MVC Controller Constructor?

Apr 22, 2010

What's the best way to handle exceptions that happen from within a controller's constructor?

All I can think of to do is use Application_OnError() or put a try/catch in my ControllerFactory.

Neither of these solutions seem ideal. Application_OnError is to broad - I have some non-mvc content in the site that has its own error handling. Using a try/catch block seems kinda hacky.

If I'm serving different content type -html/text/json/rss.... I would like to be able to handle the exception from within the action method instead of having to write all kinds of conditions to determine what kind of error message to serve.

View 1 Replies

MVC :: Model State Doesn't Contain Model Values

Aug 2, 2010

when I use Html.HiddenFor( model => model.OwnerId ) to create a hidden field, the value assigned to that field is zero. When I use <input type="hidden" value="<%: Model.OwnerId %>" /> to add the hidden field to the form, the value is assigned correctly.

Why would Html.HiddenFor( model => model.OwnerId ) not get the correct value from the Model object? Am I supposed to load model state somehow separate from returning the model object from the action method? Here is the view:

[Code]....

The Create action method is relatively straight forward.

public ActionResult Create( ... )
{
ViewStockItem item = new ViewStockItem();
item.ActionCode = ActionCode.Add;
if (item.OwnerId == 0)
item.OwnerId = 7;
BookOwner owner = db.BookOwners.Single(c => c.OwnerId == item.OwnerId);
item.OwnerName = owner.OwnerName;
return View(item);
}

View 4 Replies

Should Data Annotations Be On The Model Or The View Model

Dec 15, 2010

I've been used to decorating data model classes with data annotation attributes, but the purist in me baulks slightly at including purely presentational attributes such as display format here. I am, however, quite happy to keep validation centric attributes here. One good reason I have to continue keeping all annotations etc. in the data model is that my view model aggregates data model classes, e.g.

my ViewModelBase.DetailItem<TEntity> property in the view model is just a reference to an entity class in my data model. If I wanted to move presentational annotations to the view model, I would have to quite radically revise my design to one where I duplicate data model properties in my view model and use an object mapping tool to populate view model objects based on data model objects.

[code]....

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved