Failing to Assign values to Model/class memebers before Passing it to the view - asp.net-mvc-4

I have a Model class as follows:
namespace newWorld
{
public class oneWorld
{
//There is no direct property defined
public struct Work // struct
{
public int WorkID;
public int PersonID;
public newWorld.Person.Area.person p1;
public List<Favourite> fav;
}
public struct Favourite
{
public string chocolate {get; set;}
public string dress {get; set;}
}
}
}
=========================================
I want to access this class in controller action method so that I can assign values to list "fav" inside struct work, and access the properties and other variables and methods inside this class. HOW do i do that?
because when I do the following, inside action method:
newWorld.oneWorld obj=new newWorld.oneWorld();
obj. // does not expose anything

Related

Asp.Net Core - multiple action methods with the same name and different parameters

I'm looking for a way to have more than one action method with the same name in controller without changing Url (route).
[HTTPPost]
Public ActionResult Method1 (Dto1 param)
{
}
[HTTPPost]
Public ActionResult Method2 (Dto2 param)
{
}
[HTTPPost]
Public ActionResult Method3 (Dto3 param)
{
}
This throws error -
Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints
Dto1, Dto2 and Dto3 derive from a base Dto, each have properties specific to different request methods. I am trying to avoid having a single method with a common Dto which will require multiple validations such as validating mandatory fields based on the value of other fields, etc. If we can have 3 different Post methods with different Dtos, it would makes things much easier
Adding Dtos (Simplified)
public class BaseDto
{
public string CommonProp1 { get; set; }
public string CommonProp2 { get; set; }
}
public class Dto1: BaseDto
{
public enumType Type = enumType.Type1;
public string Property1 { get; set; }
}
public class Dto2 : BaseDto
{
public enumType Type = enumType.Type2;
public string Property2 { get; set; }
}
public class Dto3 : BaseDto
{
public enumType Type = enumType.Type3;
public string Property3 { get; set; }
}
You can use Routes or calling a private method from the three above methods, you shouldn't do this as you want. I think your problem is more deep.
But.... if you still want it, here is a workaround.
Instead of receiving an object, receive a string with json content and parse the object.
But you will have to have a property inside the "json object" or another parameter that defines you wich object it is (Dto1, Dto2 or Dto3). In any case will be the same that use different routes or methods because objects are different.
[HTTPPost]
Public ActionResult Method (string param)
{
//Decode your string called param with JSON with a property inside
}
or
[HTTPPost]
Public ActionResult Method (string param, int type)
{
//Decode your string called param with JSON switching "type" as 1, 2 or 3
}
UPDATE after your update:
I suggest you receive BaseDto and the type in other parameter.
[HTTPPost]
Public ActionResult Method (BaseDto param, int type)
{
}

How can I set the state of an object in this class hierarchy?

I have designed my classes with this hierarchy to achieve
RepositoryBase to have some functionalities that can be used by derived classes.
The derived classes implement validation and saving.
Use the IRepository interface (rather than numerous customer interfaces) to register with a IoC container to get the instance of the repository, for example CustomerRepository in this example.
The problem that I have with this design is that the customer object is not known at the construction of the CustomerRepository. It has to be set before save.
How can I redesign this hierarchy to achieve the three that mentioned at the beginning of this question?
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public void string GetFullName();
}
public interface IRepository
{
void Save();
}
public abstract class RepositoryBase<T> : IRepository
{
public abstract bool IsValid();
}
public class CustomerRepository : RepositoryBase<Customer>
{
public void Save()
{
IsValid(); // Check for validation
customer.GetFullName(); // Generate the FullName before saving
// Save the data using some RepositorBase method.
}
}

how to make httppost actionresult bind to only part of the viewModel

I have the following sudo-code
class viewModel
{
public ICollection<modelA> parentModel
public modelC formModel
}
class modelA
{
public int ID {get;set;}
public virtual Icollection<modelB> {get;set;}
}
class modelB
{
public int ID {get;set;}
public string SomeString {get;set;}
public virtual modelA ModelA {get;set;}
}
class modelC
{
public int ModelAID {get;set;}
public string SomeString {get;set;}
}
So. the view model contains a collection of As. Each A contains a collection of Bs and there is a separate model for posting back as a form: the form will be repeated on the page, once in each instance of A with the A.ID passed in to ModelAID as a hidden field. Only one form posting is allowed on the page, The id of the form fields are formModel.ModelAID and .formModel.SomeString as they are derived from the non-parent element of the viewModel.
How do I get the ActionResult to bind only to formModel?
[HttpPost]
Public ActionResult Input(formModel vm)
{
... by default the view model being passed back is full VM, I only want the formModel so the post signature does not match
}
You can try something like
public ActionResult Input([Bind(Prefix = "formModel ")]modelC model)
{
}

Add sample data to create() method

How would a programmer add sample data to a Person class so it pre-populates the fields on the view?
Here is what I'm thinking but it doesn't work:
public class Person
{
private Boolean prepopulate = false;
public Person() { if (prepopulate) { Person(prepopulate); }}
public Person(Boolean prepopulate)
{
if (prepopulate)
{
this.prepopulate = prepopulate;
SampleData.Fill(ref this);
}
}
int Id {get; set;}
string Name {get; set;}
}
My create statement might look like this:
//
// GET: /Person/Create
public ActionResult Create()
{
Person person = new Person(prepopulate=true);
return View(person);
}
The fill would do something like this reference: link.
This is all you need to do to fix your problem, whenever a new entity is created it will automatically give it the properties inside the public Person() constructor. I would highly recommend reading up on the constructors and how they work so you understand it better and understand why it works.
public class Person
{
int Id {get; set;}
string Name {get; set;}
public Person()
{
Name = "Sample";
}
}

WCF not exposing fields but uses ExtensionData

Im trying to create a WCF which consumes a List
public class myClass {
public int ID { get;set;}
}
In my service i write
[DataContract]
public class myClass
{
public int ID { get; set; }
}
And my client nows the entity
ServiceReference2.myClass[] sendData = new ServiceReference2.myClass[2];
but when i would add a new myClass to the array like
ServiceReference2.myClass add1= new ServiceReference2.myClass();
the entity only exposes the field 'ExtensionData' and not the field ID
What am i doing wrong
You forgot to decorate the ID property with the DataMember attribute:
[DataContract]
public class myClass
{
[DataMember]
public int ID { get; set; }
}