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

问题描述

大家好,



我在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 ;
  

}















谢谢








Thanks

推荐答案


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();
    }

}


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

10-31 21:38