I have a form that I binding to that has the following structure:
public class Status
{
public List<ABCAttachment> ABCAttachments_Files { get; set; }
}
public class Attachment
{
public string Id { get; set; }
}
public class ABCAttachment : Attachment
{
string Name { get; set; }
}
My action looks like this:
public ActionResult SaveAttachment(Status status)
{
....
}
The data is coming over in the form
ABCAttachments_Files[0].Id="0", ABCAttachments_Files[0].Name="test"
When I access status in my SaveAttachments action the Id is there, but the Name is not. I see it correctly being posted, but why is it not binding properly?
Looks like the Name property needs to be public or it won't be bounded to:
public class ABCAttachment : Attachment
{
string Name { get; set; }
}
should be
public class ABCAttachment : Attachment
{
public string Name { get; set; }
}
Related
I have graphql.net implementation using conventions
I have my model defined as below.
public partial class Project
{
public Project()
{
ProjectGroup = new HashSet<ProjectGroup>();
ProjectUser = new HashSet<ProjectUser>();
Datasource = new HashSet<Datasource>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ProjectGroup> ProjectGroup { get; set; }
public virtual ICollection<ProjectUser> ProjectUser { get; set; }
public virtual ICollection<Datasource> Datasource { get; set; }
}
I am trying to update only name of above class.
using above class (which is basically kind of entity framework class, but that is irrelevant of this question)
So I have defined mutation as below.
public sealed class Mutation
{
public async Task<Project> SaveProject([Inject] IProjectRepository projectRepository, projectModels.Master.Project project)
{
return Mapper.Map<Project>(await projectRepository.SaveProject(project));
}
}
and I am calling this mutation as below.
axios
.post('https://localhost:44375/api/Graph', {
query: `mutation ($project: Project) {
saveProject(project: $project) {
name
}
}`,
variables: {
'project': { 'name' : data.label },
},
})
In response I am getting below error.
{"errors":[{"message":"Variable \"project\" cannot be non-input type \"Project\".","locations":[{"line":1,"column":11}],"extensions":{"code":"VALIDATION_ERROR"}}]}
what am I doing wrong?
From graphql.net convention's official repo, I found one example and there was one attribute used for input type. After use of that it is working.
https://github.com/graphql-dotnet/conventions/blob/master/samples/DataLoaderWithEFCore/DataLoaderWithEFCore/GraphApi/Schema/InputTypes/UpdateMovieTitleParams.cs
So it requires attribute something in a following way.
[InputType]
public class UpdateMovieTitleParams
{
public Guid Id { get; set; }
public string NewTitle { get; set; }
}
I have json:
[
{
"Name" : "SHA of timestamp",
"ProjectURL" : "URL to Proj",
"AccountIdentifier" : "Account Identifier",
"Repositories":
{
"GitLink1":
{
"Login" : "login1",
"Password" : "pas1"
},
"GitLink2":
{
"Login" : "login2",
"Password" : "pas2"
},
...
"GitLink999":
{
"Login" : "login999",
"Password" : "pass999"
}
}
},
...
{
Same entry
}
]
I need to fill it in IEnumerable of my created classes
public class Git
{
public string Login { get; set; }
public string Password { get; set; }
}
public class Repositories
{
public Git git { get; set; }
}
public class ConfigEntry
{
public string Name { get; set; }
public string ProjectURL { get; set; }
public string AccountIdentifier { get; set; }
public Repositories Repositories { get; set; }
}
Using
IEnumerable<ConfigurationEntry> config = JsonConvert.DeserializeObject<IEnumerable<ConfigurationEntry>>(CONFIGJSON);
Where CONFIGJSON contains frst json file.
Gives me this data of ConfigEntry class :
Name : "filled with some data"
ProjectURL : same
AccountIdentifier : same
Repositories : NULL
How i can fill Repositories field with all data i have in config JSON?
Is there any ideas.
It looks like you probably shouldn't have a Repositories class - instead, change your ConfigEntry.Repositories property to:
public Dictionary<string, Git> Repositories { get; set; }
Then you'll get a dictionary where the key of "GetLink1" has a value with Login of "login1" etc.
Your class definitions aren't quite right.
Your "repositories" class only contains a single Git, but needs to be a Dictionary.
I think if you use this you will be close:
public class Git
{
public string Login { get; set; }
public string Password { get; set; }
}
public class ConfigEntry
{
public string Name { get; set; }
public string ProjectURL { get; set; }
public string AccountIdentifier { get; set; }
public Dictionary<string,Git> Repositories { get; set; }
}
you may also need a non-generic IEnumerable, so it knows which objects to actually create:
JsonConvert.DeserializeObject<List<ConfigurationEntry>>(CONFIGJSON);
I’m new to MVC so I’m not sure if this is the correct way of doing this. What I’m trying to do is send some JSON data to my controller via ajax, then to my model, to eventually do something to it.
The object I’m sending looks like this before I JSON it on the javascript side
bannerID:undefined
bannerMode:"WIDGET"
heightSettings:"0"
images:Array[2]
toggleArrows:"true"
toggleIndicators:"true"
transitionSpeed:""
widthSettings:"0"
When I JSON it looks like this, when I view it on my controller by replacing widget with string i get this.
{"bannerMode":"WIDGET","transitionSpeed":"","toggleArrows":"true","toggleIndicators":"true","widthSettings":"0","heightSettings":"0","images":
[{"id":0,"orderRank":"1","imageLink":"http://localhost:49999/img/admin/banner/example.jpg"},{"id":1,"orderRank":"2"}]}
In the parameters of the controller of my controller I have a widget model.
public ActionResult Index(widget widgetData){
return View("~/Views/widgets/_dynamicWidget.cshtml", widgetData);
}
My widget model looks like this.
public class widget
{
public string bannerMode { get; set; }
public int transitionSpeed { get; set; }
public bool toggleArrows { get; set; }
public bool toggleIndicators { get; set; }
public int widthSettings = 20;
private string lang;
private List<WidgetList> images1;
public widget(string lang, List<WidgetList> images1)
{
this.lang = lang;
this.images1 = images1;
}
public int heightSettings{ get; set; }
public List<ImageList> images { get; set; }
}
public class ImageList
{
public int id { get; set; }
public int orderRank { get; set; }
public string imageLink { get; set; }
}
I’m guessing the data should be set into the model ?
But in my view I’m getting a null error when I try to show this data.
Your widget class does not have a public parameterless constructor, so I'm guessing the model binder is failing there.
I have a model which is storing mycustomer new request information.
In another history model i am storing all previous request of the customer.
In view i would like to take new order and also see his previous orders and suggest some food after seeing his previous order.
Here are my models...
public class CustomerFoodModel
{
public DateTime FoodRequestCreated { get; set; }
public string FoodRequestType { get; set; }
...
...
}
public class CustomerHistoryModel
{
public string Name { get; set; }
public DateTime FoodRequestCreated { get; set; }
public string FoodRequestType { get; set; }
...
...
}
Helper.cs file
public static CustomerFoodModel getCustomerDetails(int id) // id is loyalty card number
{
// get details from (cutomer) sql table
//store it in (CustomerFoodModel)
// check if it has previous orders
getCustomerHistoryDetails(id);
....
}
public static CustomerHistoryModel getCustomerHistoryDetails(int id)
{
// get deails from (history) sql table
// store it in (CustomerHistoryModel
}
In my controller, I am passing my (CustomerFoodModel) to the view.
public ActionResult EditCustomerRequest(int id, string name, string date)
{
CustomerFoodModel CRequest = Helper.getCustomerDetails(id);
...
return PartialView("EditCustomerRequest",CRequest);
}
How do I show the (CustomerHistoryModel) in the same view.? Is there possible to include (CustomerHistoryModel) in (CustomerFoodModel)?
Create a new class to wrap both of the model.
public class CustomerFoodModel
{
public CustomerFoodModel CustomerFood { get; set; }
public CustomerHistoryModel CustomerHistory { get; set; }
}
And on your controller
public ActionResult EditCustomerRequest(int id, string name, string date)
{
CustomerFoodModel CRequest = Helper.getCustomerDetails(id);
CustomerHistoryModel CHModel = Helper. getCustomerHistoryDetails(id);
return PartialView("EditCustomerRequest",new CustomerFoodModel(){
CustomerFood = CRequest,
CustomerHistory = CHModel
});
}
I think the best approach is to use a partial view inside the main view. The partial view can call back to another controller to get a new model and pass that model to the partial view. This keeps things better seperated.
Look at this post for a similar issue.
Using partial views in ASP.net MVC 4
Use wrapper class which contain both of class
public class CustomerViewModel
{
public CustomerFoodModel FoodModel { get; set; }
public CustomerHistoryModel HistoryModel { get; set; }
}
You have a few options. I would probably could create a view model that contains both of your models:
public class CustomerViewModel
{
public CustomerFoodModel FoodModel { get; set; }
public CustomerHistoryModel HistoryModel { get; set; }
}
Or, depending on your data structure, you may have multiple history entries per customer:
public class CustomerViewModel
{
public CustomerFoodModel FoodModel { get; set; }
public List<CustomerHistoryModel> HistoryModels { get; set; }
}
Then your getCustomerDetails function would return a CustomerViewModel instead.
My task is to show multiple models into a single view.I've created a ViewModel for my requirement but I'm not meeting my requirement.
please have a look into the below code and rectify me where m i going wrong ???
public partial class StudentsDetail
{
public int StudentID { get; set; }
public int ParentID { get; set; }
public string StudentName { get; set; }
public string Gender { get; set; }
public string FatherName { get; set; }
public string MotherName { get; set; }
public Nullable<System.DateTime> DateOfBirth { get; set; }
public virtual ParentsDetail ParentsDetail { get; set; }
public virtual SchoolDetail SchoolDetail { get; set; }
}
//Model 2
public partial class ParentsDetail
{
public ParentsDetail()
{
this.StudentsDetails = new HashSet<StudentsDetail>();
}
public int ParentID { get; set; }
public string Occupation { get; set; }
public string Organization { get; set; }
public string AnnualIncome { get; set; }
public virtual ICollection<StudentsDetail> StudentsDetails { get; set; }
}
//ViewModel Which I have created
public class ParentsInformationViewModel
{
public List<StudentsDetail> StudentsDetails { get; set; }
public List<ParentsDetail> ParentsDetails { get; set; }
public ParentsInformationViewModel(List<StudentsDetail> _studentDetails, List<ParentsDetail> _parentsDetails) //Should i pass all the required parameters that i want to display in view ????
{
StudentsDetails = _studentDetails;
ParentsDetails = _parentsDetails;
}
//And finally this is my method defined in the StudentController (Have i defined it in a right place/way??)
public ActionResult StudentViewModel()
{
ViewBag.ParentsDetail = new ParentsDetail(); //ParentsDetail is my controller
List<StudentsDetail> studentListObj = StudentsDetailsDAL.GetStudentDetails();
List<ParentsInformationViewModel> ParentInfoVMObj = new List<ParentsInformationViewModel>();
//foreach (var student in studentListObj)
//{
// ParentInfoVMObj.Add(new ParentsInformationViewModel(student.StudentID, student.ParentID));
//}
//ParentInfoVMObj.Add(ParentInfoVMObj); /// don't know how to call the required viewmodel
return View(ParentInfoVMObj);
}
I know that the above method of a ViewModel is wrong but how to use it or where am i going wrong I can't get.
I want to display the ViewModel in the view as a detailed view .
Please, correct me as I'm a starter in MVC3 .
Thanks In Advance!!
In your controller, define your action method as follows.
public ActionResult ParentsDetails()
{
var studentDetails = new List<StudentDetail>();
var parentDetails = new List<ParentsDetail>();
// Fill your lists here, and pass them to viewmodel constructor.
var viewModel = new ParentsInformationViewModel(studentDetails, parentDetails)
// Return your viewmodel to corresponding view.
return View(viewModel);
}
In your view define your model.
#model MySolution.ViewModels.ParentsInformationViewModel
Is there in your view declared that you are receiving model of type
In view:
#model IEnumerable<ParentsInformationViewModel>