我正在制作一个项目,我必须与房间相互比较,如果房间有变化则打印。
我制作了这段代码,工作正常。但代码真的很难看,我想优化代码。有没有办法可以将变量收集在一起,所以我没有14行变量?也许我以后必须使用变量,所以我不想内联变量。
<th class="callout-inner secondary no-changes-reservation-bgcolor">
<h5>Reservationsnummer:</h5>
@{
var orderfirstName = Model.Order.FirstName.ToString();
var order2firstName = Model.Order2.FirstName.ToString();
var orderlastName = Model.Order.LastName.ToString();
var order2lastName = Model.Order2.LastName.ToString();
var orderaddress = Model.Order.Address.ToString();
var order2address = Model.Order2.Address.ToString();
var orderpostCode = Model.Order.PostCode.ToString();
var order2postCode = Model.Order2.PostCode.ToString();
var orderCity = Model.Order.City.ToString();
var order2City = Model.Order2.City.ToString();
var orderPhone = Model.Order.Phone.ToString();
var order2Phone = Model.Order2.Phone.ToString();
var orderEmail = Model.Order.Email.ToString();
var order2Email = Model.Order2.Email.ToString();
}
<table align="center" class="wrapper header float-center">
<tr>
<td class="wrapper-inner background-color__yellow">
<span>
@renderOrderPropertyBlock("Fornavn", orderfirstName, order2firstName)
<br />
@renderOrderPropertyBlock("Efternavn", orderlastName, order2lastName)
<br />
@renderOrderPropertyBlock("Adresse", orderaddress, order2address)
<br />
@renderOrderPropertyBlock("Postnummer", orderpostCode, order2postCode)
<br />
@renderOrderPropertyBlock("City", orderCity, order2City)
<br />
@renderOrderPropertyBlock("Phone", orderPhone, order2Phone)
<br />
@renderOrderPropertyBlock("Email", orderEmail, order2Email)
</span>
</td>
</tr>
</table>
</th>
答案 0 :(得分:1)
您不必将这些变量,简单传递属性值定义到您的html帮助器:
@renderOrderPropertyBlock("Fornavn", Model.Order.FirstName.ToString(), Model.Order2.FirstName.ToString())
您还可以使用反射迭代属性,但代码执行速度会慢得多。
答案 1 :(得分:1)
你可以这样做:
@{
var order1 = Model.Order;
var order2 = Model.Order2;
}
在你的范围内:
<span>
@renderOrderPropertyBlock("Fornavn", order1.FirstName.ToString(), order2.FirstName.ToString())
<br />
// and so on ...
</span>