C#-在三元运算符中渲染局部

时间:2019-12-13 19:35:12

标签: c# asp.net partial-views conditional-operator

我有条件地使用三元运算符渲染页脚。我正在做@RenderPage,即使它工作正常,也意味着要有一个控制器以及其他一些额外的代码。

我遇到了; expected错误,根据StackOverflow和一些文档,这是一个通用错误,可能是由许多不同的原因引起的。

我的语法正确吗?还是我想念的东西?


// in this section is a switch statement that sets isNewFooter to true or false depending on which page has loaded.

 <div class="body-content">
    @RenderBody()

    @{
       (isNewFooter ? Html.RenderPartial("~/Views/Shared/NewFooter.cshtml") : Html.RenderPartial("~/Views/Shared/OldFooter.cshtml"))
     }

</div>

1 个答案:

答案 0 :(得分:6)

三元运算符用于评估不同的表达式,而不是执行不同的语句。您可以只使用标准的public class AudioSessionReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int id = intent.getIntExtra(Equalizer.EXTRA_AUDIO_SESSION, -1); String packageName = intent.getStringExtra(Equalizer.EXTRA_PACKAGE_NAME); } }

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Paytoll_Management_System
{
    class DBConnection
    {
        public SqlConnection con;
        public SqlCommand cmd;
        public SqlDataAdapter sda;
        public string pkk;

        public void Connection()
        {
            con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=PayrollDB;Integrated Security=True;"); //datasource=127.0.0.1;port=3306;username=root;password=;database=payrolldb
            con.Open();
        }

        public void DataSend(string SQL)
        {
            try
            {
                Connection();
                SqlTransaction trans = con.BeginTransaction();
                cmd = new SqlCommand(SQL, con);
                cmd.ExecuteNonQuery();
                trans.Commit();
                pkk = "";
            }
            catch
            {
                pkk = "Connection Failed!";
            }
            con.Close();
        }

        public void DataGet(string SQL)
        {
            try
            {
                Connection();
                sda = new SqlDataAdapter(SQL, con);
            }
            catch(Exception)
            {

            }
        }

    }
}

或者,重构常见的东西,以便您可以使用三元运算符:

private void btnInsert_Click(object sender, EventArgs e)
    {
        if (Validation())
        {
            if (IfUsernameExists(txtUsername.Text))
            {
                MessageBox.Show("Username already exists!", "Message", MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
            }
            else
            {
                con.DataSend("INSERT INTO [Users] (name, email, username, password, role, dob, address) VALUES('" + txtName.Text + "','" + txtEmail.Text + "','" + txtUsername.Text + "','" + txtPassword.Text + "','" + cbRole.Text + "','" + dtDob.Value.ToString("dd/MM/yyyy") + "','" + txtAddress.Text + "')");
                MessageBox.Show("Record Saved Succesfully!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Clear_Data();
                LoadData();
            }
        }
    }

使用其中任何一个都更易于阅读和维护。