从包含id的if else语句中打印出来

时间:2017-04-03 18:36:00

标签: c# asp.net-mvc razor

我已经完成了这个功能。我正在使用Razor和C#。如果订单的SupplierId是以下数字之一,我必须打印出属于SupplierId的文本:

@{
    var pointShop = Model.Order.OrderLines[0];
    string supplier == null;

    if (pointShop.SupplierId = 27942) {
    supplier = "If any question, please contact us";
    }
    else if (pointShop.SupplierId = 6543) {
    supplier = "Please call us";
    }
    else if (pointShop.SupplierId = 8723) {
    supplier = "You are welcome to write us";
    }
}

我打印出这样的供应商:

<span>
   @supplier
</span>

在我的订单中有100%的ID有27942.但我没有得到文本打印。只是一个空场。我在这里做错了吗?

2 个答案:

答案 0 :(得分:1)

您似乎错过了使用===,因为其他人也不知道。

@{
    var pointShop = Model.Order.OrderLines[0];
    string supplier == null;

    if (pointShop.SupplierId = 27942) {
    supplier = "If any question, please contact us";
    }
    else if (pointShop.SupplierId = 6543) {
    supplier == "Please call us";
    }
    else if (pointShop.SupplierId = 8723) {
    supplier == "You are welcome to write us";
    }
}

让我们逐行。

 string supplier == null;

这不应该编译,所以我不确定你实际上在运行什么。我认为您打算创建一个空字符串变量,正确的方法是:

string supplier = String.Empty;

var supplier = String.Empty;

然后在你的if语句中进行平等检查,你做的是分配而不是评估。 =是赋值==是评价。

if (pointShop.SupplierId = 27942) {
    supplier = "If any question, please contact us";
    }

在if语句中,pointShop.SupplierId将被赋值为27942,而不是评估SupplierId并检查它对27942的价值。你需要

if (pointShop.SupplierId == 27942) {
    supplier = "If any question, please contact us";
    }

代替。

所有这一切都可以在调试器中通过在剃刀视图中放置断点并踩到它来检查。祝你好运!

答案 1 :(得分:0)

如果你有一堆,如果所有检查相同的值,一个开关...案例可能是更清洁的解决方案。从那时起的下一步将是数组或<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="@android:color/white"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/groupID" android:textSize="20sp" android:layout_marginBottom="15dp" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"> <LinearLayout android:id="@+id/layout_add_new_member" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"> <ImageView app:srcCompat="@drawable/add" android:id="@+id/imageView2" android:layout_marginLeft="16dp" android:layout_width="80dp" android:layout_height="60dp" android:layout_weight="1" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:id="@+id/addMember" android:text="Add New Members" android:textSize="18sp" android:textColor="@android:color/holo_red_dark" android:layout_weight="1" android:layout_gravity="center_vertical" /> </LinearLayout> <ListView android:id="@+id/ListView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_above="@id/layout_add_new_member" android:background="@android:color/white" /> </RelativeLayout> <Button android:text="Leave this group" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/leaveGroupButton" android:background="@color/colorPrimary" android:layout_gravity="bottom|end" /> </LinearLayout>

您可以使用比较运算符,而不是赋值运算符:

Dictionary<int, string>

这将始终为false,因为null不等于任何字符串。但这将是假的;对编译器没有任何意义。