In asp.net mvc 3.0, I want to Deserialize and object which is stored in an input hidden filed.
Here is the Serializable class :
[Serializable]
public class RegistrationData
{
public string Name { get; set; }
public string Email { get; set; }
}
and the this is how i generate hidden field :
<%: Html.Serialize("regData", Model,
Microsoft.Web.Mvc.SerializationMode.Signed)%>
but when I Deserialize it through following method, I get null values for my object :
var serialized = Request.Form["regData"];
if (serialized != null)
{
regData = (RegistrationData)newMvcSerializer().Deserialize(serialized,SerializationMode.Signed);
}
Can any one help me please?
Model
public class RegistrationData
{
public string Name { get; set; }
public string Email { get; set; }
}
View
#using (Html.BeginForm())
{
#Html.HiddenFor(m=>m.Name)
#Html.HiddenFor(m=>m.Email)
}
Controller
public ActionResult Foo(RegistrationData form)
{
//do
//form.Name ->> get name
}
Related
Model:
[DataContract]
public class Employee
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[DataMember(Name ="id")]
public int Id{ get; set; }
[DataMember(Name = "fullName")]
public string FullName { get; set; }
}
[DataContract]
public class Department
{
public Department()
{
this.Employees = new List<Employee>();
}
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[DataMember(Name = "id")]
public int Id { get; set; }
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "employees")]
public List<Employee> Employees { get; set; }
}
Controller
public HttpResponseMessage Get([FromUri]Department model)
{
if (ModelState.IsValid)
{
}
return new HttpResponseMessage(HttpStatusCode.OK);
}
Url : "http://localhost:2070/home/get/?id=1&name=IT&Employees=1,John"
I am trying to invoke above URL and the Model does not read the Employees. Other property like int,double,string,decimal are read by the Model.
Can anyone help me on what is the correct format in passing List thru Url.
Also, I dont want to decorate each of my class with modelbinders nor the parameter in my controller.
Tech : WebApi, .Net3.5
You need to specify the index of the list and property to bind with when using FromUri and list/array
Try it this way
http://localhost:2070/home/get/?id=1&name=IT&Employees[0].Id=1&Employees[0].Name=John
The parameters dictionary contains a null entry for parameter 'i' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'MvcDemo.Controllers.EmployeeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
This is my controller :
public class EmployeeController : Controller
{
//
// GET: /Employee/
public ActionResult Details(int i)
{
EmployeeContext ec = new EmployeeContext();
Employee employee = ec.emp.Single(ep => ep.empid == i);
return View(employee);
}
}
This is my model class :
[Table("employee")]
public class Employee
{
public int empid { get; set; }
public string name { get; set; }
public string gender { get; set; }
public string add { get; set; }
public string country { get; set; }
public string state { get; set; }
public string city { get; set; }
}
This is DbContext class :
public class EmployeeContext : DbContext
{
public DbSet<Employee> emp { get; set; }
}
Please tell me what is above error and why it is coming?
I am new to MVC4, I want to implement WebGrid, When retrieving data for model.activity its working fine. For clarification i am combining two tables to get data.
Here its showing error like this
Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List
Please help me to solve this problem. thanks in advance
public class MyViewModel
{
public List<Tbl_Activity> activity;
public List<Tbl_Clarification> clarification;
}
public class ClarificationEntities
{
public int ClrNo { get; set; }
public Nullable<int> DailyReportID { get; set; }
public string ReportingMgrComment { get; set; }
public Nullable<System.DateTime> CreatedOn { get; set; }
public string StaffComment { get; set; }
public Nullable<int> CreatedBy { get; set; }
public string Name { get; set; }
}
I am adding data to the model to display in WebGrid
MyViewModel model = new MyViewModel();
model.activity = db.Tbl_Activity.Where(x => x.DailyReportID == driD).ToList();
model.clarification = (from c in db.Tbl_Clarification
join u in db.Tbl_Users on c.CreatedBy equals u.CreatedBy
where c.DailyReportID == did
select new ClarificationEntities
{
ClrNo = c.ClrNo,
ReportingMgrComment = c.ReportingMgrComment,
StaffComment = c.StaffComment,
DailyReportID=c.DailyReportID,
Name=u.Name
}).ToList();
return View(model);
MyViewModel has the wrong type for the clarification field...
try the following instead
public class MyViewModel
{
public List<Tbl_Activity> activity;
public List<ClarificationEntities> clarification;
}
I am trying to update a compound page model which as one of its properties has a list of objects.
My Model looks like this:
public class PageViewModel
{
public ProgramListVM ProgramsDDL { get; set; }
public PageViewModel()
{
this.ProgramsDDL = new ProgramListVM();
}
}
The ProgramListVM class is:
public class ProgramListVM
{
public List<ProgramVM> Program_List { get; set; }
public int SelectedValue { get; set; }
public ProgramListVM()
{
this.Program_List = new List<ProgramVM>();
this.SelectedValue = 0;
}
}
and ProgramVM is:
public class ProgramVM
{
public int ProgramID { get; set; }
public string ProgramDesc { get; set; }
public ProgramVM(int id, string code)
{
this.ProgramID = id;
this.ProgramDesc = code;
}
}
I try to render this dropdownlist by the following two:
1-
<%: Html.DropDownList("ProgramsDDL", new SelectList(Model.Page6VM.ProgramsDDL.Program_List, "ProgramID", "ProgramDesc", Model.Page6VM.ProgramsDDL.SelectedValue))%>
2-
<%: Html.DropDownListFor(m => m.Page6VM.ProgramsDDL.Program_List, new SelectList(Model.Page6VM.ProgramsDDL.Program_List, "ProgramID", "ProgramDesc"), Model.Page6VM.ProgramsDDL.SelectedValue)%>
But when I try to update my model through a controller action
[HttpPost]
public ActionResult UpdateUser(PageViewModel model)
{
}
model.ProgramsDDL.count is zero.
What is the best way to render this dropdownlist and be able to set the selected index, and also be able to send the selected index back to the controller?
You mixed up the parameters for Html.DropDownListFor(). Code sample below should work.
<%: Html.DropDownListFor(m => m.SelectedValue,
new SelectList(Model.Page6VM.ProgramsDDL.Program_List, "ProgramID", "ProgramDesc"),
null) %>
You also should have a SelectedValue in your model that's posted back.
public class PageViewModel
{
public ProgramListVM ProgramsDDL { get; set; }
public int SelectedValue { get; set; }
public PageViewModel()
{
this.ProgramsDDL = new ProgramListVM();
}
}
Also default model binder can't map complex collections to your model. You probably don't need them in your post action anyway.
I am developing an asp.net mvc application, which has these enity classes:
public class Person
{
public int PersonID { get; set; }
public string PersonPicAddress { get; set; }
public virtual List<Person_Local> PersonLocal { get; set; }
}
public class Person_Local
{
public int PersonID { get; set; }
public int CultureID { get; set; }
public string PersonName { get; set; }
public string PersonFamily { get; set; }
public string PersonAbout { get; set; }
public virtual Culture Culture { get; set; }
public virtual Person Person { get; set; }
}
public class Culture
{
public int CultureID { get; set; }
[Required()]
public string CultureName { get; set; }
[Required()]
public string CultureDisplay { get; set; }
public virtual List<HomePage> HomePage { get; set; }
public virtual List<Person_Local> PersonLocak { get; set; }
}
I defined an action with [Httppost] attribute, which accepts complex object from a view.
Here is the action :
[HttpPost]
public ActionResult CreatePerson([Bind(Prefix = "Person")]Person obj)
{
AppDbContext da = new AppDbContext();
//Only getting first PersonLocal from list of PersonLocals
obj.PersonLocal[0].Person = obj;
da.Persons.Add(obj);
da.SaveChanges();
return Jsono(...);
}
But when it throws error as below :
Exception:Thrown: "Invalid column name 'Culture_CultureID'." (System.Data.SqlClient.SqlException)
A System.Data.SqlClient.SqlException was thrown: "Invalid column name 'Culture_CultureID'."
And the insert statement :
ADO.NET:Execute Reader "insert [dbo].[Person_Local]([PersonID], [PersonName], [PersonFamily], [PersonAbout], [Culture_CultureID])
values (#0, #1, #2, #3, null)
select [CultureID]
from [dbo].[Person_Local]
where ##ROWCOUNT > 0 and [CultureID] = scope_identity()"
The command text "insert [dbo].[Person_Local]([PersonID], [PersonName], [PersonFamily], [PersonAbout], [Culture_CultureID])
values (#0, #1, #2, #3, null)
select [CultureID]
from [dbo].[Person_Local]
where ##ROWCOUNT > 0 and [CultureID] = scope_identity()" was executed on connection "Data Source=bab-pc;Initial Catalog=MainDB;Integrated Security=True;Application Name=EntityFrameworkMUE", building a SqlDataReader.
Where is the problem?
Edited:
Included EntityConfigurations Code:
public class CultureConfig : EntityTypeConfiguration<Culture>
{
public CultureConfig()
{
HasKey(x => x.CultureID);
Property(x => x.CultureName);
Property(x => x.CultureDisplay);
ToTable("Culture");
}
}
public class PersonConfig : EntityTypeConfiguration<Person>
{
public PersonConfig()
{
HasKey(x => x.PersonID);
Property(x=>x.PersonPicAddress);
ToTable("Person");
}
}
public class Person_LocalConfig : EntityTypeConfiguration<Person_Local>
{
public Person_LocalConfig()
{
HasKey(x => x.PersonID);
HasKey(x => x.CultureID);
Property(x=>x.PersonName);
Property(x => x.PersonFamily);
Property(x => x.PersonAbout);
ToTable("Person_Local");
}
}
Try to remove fields CultureID and PersonID from Person_Local class. Because you already has field Person and Culture
It looks like your schema is out of sync with your model. Make sure you understand EF Code first schema update features, which are described in this blog. If you need more sophisticated schema migration, there are some other approaches in answers to this question.