获取具有模型值的完整对象。 [英] Get the Full Object with Value of the Model.

查看:114
本文介绍了获取具有模型值的完整对象。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在MVC 4 Razor中创建模型。



公共课员工

{

public int employeeid {get; set;}

public Name EmployeeName {get; set;}

public Billing_Address EmpBillingAddress {get; set;}

}



我创建了一个View EmployeeCreation 我将这个视图绑定到Employee对象。



但是当我提交此视图时。在帖子中我无法获得Billing_Address对象的值。





所以我需要一些相同主题的协调。因为还有一些属性是类对象属性。





Hi Everybody,

I have Create a Model in MVC 4 Razor.

public class Employee
{
public int employeeid {get;set;}
public Name EmployeeName {get;set;}
public Billing_Address EmpBillingAddress {get;set;}
}

I have create a View EmployeeCreation in which have i am binding this view to Employee object.

but when i am submit this view . In post i am unable to get the values of Billing_Address Object value.


So i need some co ordinations for the same topics. Because there are some more Properties which are class object properties.


//MyController Code


Public ActionResult Create ()
{
   return view ;
}

[HttpPost]
Public ActionResult Create (Employee Emp1)
{

 Billing_Address b=  Emp1.Billing_Address ;
  

}



///这里B的值始终为空











谢谢


/// Here value of B is always null





Thanks

推荐答案

最初创建时的任何对象/属性都将为null。发布后它会是一样的。无论在视图中提交/发布的任何值,都将在发布后的操作后返回给控制器。例如,



查看:









在控制器中:

[HttpPost]

公共ActionResult创建(员工Emp1)

{



Billing_Address b = Emp1.EmpBillingAddress;





}



PS:更改名称。因为在Employee类中,有一个属性EmpBillingAddress。

我认为这就是为什么它显示为null。改变它并尝试。您再次尝试访问该类而不是属性。请告诉它是否解决..
Initially any object/property when created will be null. after post it will be the same. Whatever values have been submitted/posted in view only will be returned to the controller after a post action. for example,

In View :




In controller :
[HttpPost]
Public ActionResult Create (Employee Emp1)
{

Billing_Address b= Emp1.EmpBillingAddress;


}

P.S : Change the name. Because in the Employee class, there is a property EmpBillingAddress.
I think that is why it is showing null. Change it and try. You are once again trying to access the class and not the property. Please tell if it solves..


你的模特



Your Models

public class Employee
{
    public int ID { get; set; }
    public String Name { get; set; }
    public BillingAddress billingAddress { get; set; }
}
public class BillingAddress
{
    public int ID { get; set; }
    public int HouseNo { get; set; }
    public String Street { get; set; }
    public String Locality { get; set; }
    public String City { get; set; }
    public String District { get; set; }
    public String State { get; set; }
    public String Country { get; set; }
    public int PinCode { get; set; }
}





并查看



@model FormAuth.Models.Employee

@ {

ViewBag.Title =Add;

}



添加

@using(Html.BeginForm())

{

@ Html.ValidationSummary(true)





员工



@ Html.LabelFor(model => model.Name)





@ Html.EditorFor(model => model.Name)

@ Html.ValidationMessageFor(model => model.Name)





BillingAddress



@ Html.LabelFor(model => model.billingAddress.HouseNo)





@ Html.EditorFor(model => model.billingAddress.HouseNo)

@ Html.ValidationMessageFor(model => model.billingAddress.HouseNo)





@ Html.LabelFor(model => model.billingAddress.Street)





@ Html.EditorFor(model => model.billingAddress.Street)

@ Html.ValidationMessageFor(model => ; model.billingAddress.Street)





@ Html.LabelFor(model => model.billingAddress.Locality)





@ Html.EditorFor(model => model.billingAddress.Locality)

@ Html.ValidationMessageFor(model = > model.billingAddress.Locality)





@ Html.LabelFor(model => model.billingAddress.City)





@ Html.EditorFor(model => model.billingAdd ress.City)

@ Html.ValidationMessageFor(model => model.billingAddress.City)





@ Html.LabelFor(model => model.billingAddress.District)





@ Html.EditorFor(model => model.billingAddress.District)

@ Html.ValidationMessageFor(model => ; model.billingAddress.District)





@ Html.LabelFor(model => model.billingAddress.State)





@ Html.EditorFor(model => model.billingAddress.State)

@ Html.ValidationMessageFor(model = > model.billingAddress.State)





@ Html.LabelFor(model => model.billingAddress.Country)





@ Html.EditorFor(model => model.billingAddres s.Country)

@ Html.ValidationMessageFor(model => model.billingAddress.Country)





@ Html.LabelFor(model => model.billingAddress.PinCode)





@ Html.EditorFor(model => model.billingAddress.PinCode)

@ Html.ValidationMessageFor(model => ; model.billingAddress.PinCode)













}



@ Html.ActionLink(返回列表,索引)



@section脚本{

@ Scripts.Render(〜/ bundles / jqueryval)

}



您只能使用HTML语法编写视图,而不是使用razor语法。但要注意控件的名称(As Laxmi Said)。







And View

@model FormAuth.Models.Employee
@{
ViewBag.Title = "Add";
}

Add
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)


Employee

@Html.LabelFor(model => model.Name)


@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)


BillingAddress

@Html.LabelFor(model => model.billingAddress.HouseNo)


@Html.EditorFor(model => model.billingAddress.HouseNo)
@Html.ValidationMessageFor(model => model.billingAddress.HouseNo)


@Html.LabelFor(model => model.billingAddress.Street)


@Html.EditorFor(model => model.billingAddress.Street)
@Html.ValidationMessageFor(model => model.billingAddress.Street)


@Html.LabelFor(model => model.billingAddress.Locality)


@Html.EditorFor(model => model.billingAddress.Locality)
@Html.ValidationMessageFor(model => model.billingAddress.Locality)


@Html.LabelFor(model => model.billingAddress.City)


@Html.EditorFor(model => model.billingAddress.City)
@Html.ValidationMessageFor(model => model.billingAddress.City)


@Html.LabelFor(model => model.billingAddress.District)


@Html.EditorFor(model => model.billingAddress.District)
@Html.ValidationMessageFor(model => model.billingAddress.District)


@Html.LabelFor(model => model.billingAddress.State)


@Html.EditorFor(model => model.billingAddress.State)
@Html.ValidationMessageFor(model => model.billingAddress.State)


@Html.LabelFor(model => model.billingAddress.Country)


@Html.EditorFor(model => model.billingAddress.Country)
@Html.ValidationMessageFor(model => model.billingAddress.Country)


@Html.LabelFor(model => model.billingAddress.PinCode)


@Html.EditorFor(model => model.billingAddress.PinCode)
@Html.ValidationMessageFor(model => model.billingAddress.PinCode)






}

@Html.ActionLink("Back to List", "Index")

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

you can write your view in HTML syntax only, instead of razor syntax. But take care of name of the controls (As Laxmi Said).


public class EmployeeController : Controller
{
    //
    // GET: /Emplyee/

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Add()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Add(FormAuth.Models.Employee employee)
    {
        return View();
    }

}


这篇关于获取具有模型值的完整对象。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆