DropDownList的选定值未显示

时间:2017-06-05 12:17:53

标签: c# asp.net-mvc drop-down-menu

我正在尝试为我的下拉列表设置一个选定的值,但我无法在页面上显示它。

这是我设置所选值的方式;

public class ContactUs extends AppCompatActivity {
private EditText name, phone, email, msg;
private Button submitButton;
private TextInputLayout inputLayoutName, inputLayoutPhone, inputLayoutEmail, inputLayoutMessage;


EditText editText;


//New Code
Session session = null;
ProgressDialog pdialog = null;
Context context = null;
//private EditText name, phone, email, message;
String Name, Phone, Email, Msg;
String receiver = "xyz@gmail.com";
String sender = "abc@gmail.com";
String subject = "Test Mail";
String textMessage = "Hello To All";
String Data;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_us);

    initializeWidgets();


    submitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean isValid = true;
            if (name.getText().toString().isEmpty()) {
                inputLayoutName.setError("Name is mandatory");
                isValid = false;
            } else {
                inputLayoutName.setErrorEnabled(false);
            }

            if (phone.getText().toString().isEmpty()) {
                inputLayoutPhone.setError("Phone number is mandatory");
                isValid = false;
            } else {
                inputLayoutPhone.setErrorEnabled(false);
            }

            if (email.getText().toString().isEmpty()) {
                inputLayoutEmail.setError("Email is required");
                isValid = false;
            } else {
                inputLayoutEmail.setErrorEnabled(false);
            }


            if (isValid) {
                                String Name = name.getText().toString();
                String Phone = phone.getText().toString();
                String Email = email.getText().toString();
                String Message = msg.getText().toString();

                Data = Name + Phone + Email + Message;

                Properties prop = new Properties();
                prop.put("mail.smtp.host", "smtp.gmail.com");
                prop.put("mail.smtp.socketFactory.port", "587");
                prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                prop.put("mail.smtp.auth", "true");
                prop.put("mail.smtp.port", "587");

                session = Session.getDefaultInstance(prop, new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        PasswordAuthentication passwordAuthentication = new PasswordAuthentication(sender, "abc");
                        return passwordAuthentication;
                    } });

                try {
                    MimeMessage message = new MimeMessage(session);
                    message.setFrom(new InternetAddress(sender));
                    message.setRecipients(MimeMessage.RecipientType.TO, InternetAddress.parse(receiver));
                    message.setText(Data);
                    Transport.send(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
            name.setText("");
            phone.setText("");
            email.setText("");
            msg.setText("");
            Toast.makeText(getApplicationContext(), "Data Submitted Successfully", Toast.LENGTH_LONG).show();
        }

    });
}

private void initializeWidgets() {

    name = (EditText) findViewById(R.id.name);
    phone = (EditText) findViewById(R.id.phone);
    email = (EditText) findViewById(R.id.email);
    msg = (EditText) findViewById(R.id.message);
    submitButton = (Button) findViewById(R.id.btnSubmit);
    inputLayoutName = (TextInputLayout) findViewById(R.id.inputLayoutName);
    inputLayoutPhone = (TextInputLayout) findViewById(R.id.inputLayoutPhone);
    inputLayoutEmail = (TextInputLayout) findViewById(R.id.inputLayoutEmail);
    inputLayoutMessage = (TextInputLayout) findViewById(R.id.inputLayoutMessage);

}


public boolean onSupportNavigateUp() {
    onBackPressed();`enter code here`
    return true;
}

这是我视图中的一部分

productHier = new SelectList(hierarchyList, "ProductHierarchyID", "ProductHierarchyCode", orderline.PrmProductVariant.PrmProduct.PrmProductHierarchy1 );
ViewBag.productHier = productHier;

我从其他问题上尝试过很多关于这个问题的建议,但没有人帮助我。我想知道我错过了什么。

编辑:当我调试时,我可以在控制器和视图中看到指定为选定值的值,但不知何故我在页面上看不到它们。

2 个答案:

答案 0 :(得分:0)

要绑定Hierarchy值,您需要使用DropDownListFor HtmlHelper,如下所示:

@Html.DropDownListFor(m=>m.Hierarchy, (IEnumerable<SelectListItem>)ViewBag.productHier, "Hierarchy Seçiniz", new { @Class = "form-control control-text" })

返回View时,您必须在控制器中设置Hierarchy属性。

答案 1 :(得分:0)

我认为按照你的方式,你需要这样做:

@Html.DropDownList("productHier", null, "Hierarchy Seçiniz", new { @Class = "form-control control-text" })

请注意,这种创建下拉列表的方式不适用于验证。

您必须使用DropDownListFor才能获得正确的验证。

希望这有帮助!