在MVC中使用两个实体模型列创建TextBoxfor [英] Create the TextBoxfor With Two entity model columns in MVC

查看:26
本文介绍了在MVC中使用两个实体模型列创建TextBoxfor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我解释了我的情况,我已经在数据库中创建了像CountryCode,PhoneNumber这样的列并创建模型和控制器

1st I explain my scenario, I have created the Columns in DB Like CountryCode,PhoneNumber and create the Model and Controller

在视图中,我们需要在文本框中显示CountryCode以及像这样的电话号码(+91 9856254874)

In View we need to show the CountryCode along with PhoneNumber like this(+91 9856254874) in textbox

当用户更改文本框值并提交时,我需要通过拆分代码和电话号码来分别更新两列

when user changed the textbox value and submit i need to update respective two columns by splitting the code and phone number

我该如何实现?

这是我的模特:

 public partial class Details
 {
    public Nullable<int> ContryCode { get; set; }
    public string        PhoneNumber{ get; set; }
 }

这是我对View的期望,但它不起作用

This is my expectation on View But its not working

 @Html.TextBoxFor(model => model.ContryCode+" "+ model => model.PhoneNumber, new { placeholder = "Mobile Number", @readonly = "readonly", @class = "mobilenumber",@id="MobileNumber"})

当我单击提交"按钮时,此文本框位于@ Html.BeginForm内,我需要使用文本框中的ContryCode和PhoneNumber分开来更新数据库

This Textbox located inside the @Html.BeginForm when i click the submit button i need to update the DB with Separated ContryCode and PhoneNumber from the textbox

请帮助我

预先感谢

推荐答案

我看到的最简单的方法是为两个字段创建两个不同的文本框.

The easiest way that I can see is to just create two different textboxes for the two fields.

但是,您还可以创建一个带有字符串属性的ViewModel,该属性将ContryCode和PhoneNumber连接起来,如下所示:

However, you can also create a ViewModel with a string property that concatenates the ContryCode and PhoneNumber, something like this:

public partial class DetailsViewModel
{
    public Nullable<int> ContryCode { get; set; }
    public string PhoneNumber{ get; set; }
    public string PhoneNumberWithContryCode 
    {
         get
         {
             return string.Format("+{0} {1}", ContryCode, PhoneNumber);
         }
         set
         {
              // Code to parse the string to ContryCode and PhoneNumber
         }
}

但是,对于此选项,尤其是验证,您还有很多要考虑的问题.如果没有+怎么办?如果没有空间怎么办?等等.

However, you would have a lot more to think about for this option, specifically validation. What if there's no +? What if there's no space? Etc.

如果JavaScript是公平的游戏,则可以使用原始的View,但可以将ContryCode和PhoneNumber属性绑定到隐藏字段.然后在表格中包含一个文本框,在其中您将ContryCode和PhoneNumber的值连接在一起.当此文本框更改值时,请使用一些JavaScript代码更新隐藏字段的值.从本质上讲,这仍然与上面的建议相似,只是将解析/验证移到了纯JavaScript.

If JavaScript is fair game, you can use your original View, but bind your ContryCode and PhoneNumber properties to hidden fields. And then include a textbox in the form where you concatenate the values of ContryCode and PhoneNumber. When this textbox changes value, have some JavaScript code update the values of the hidden fields. This is essentially still similar to the suggestion above, with the parsing/validating moved to pure JavaScript.

我仍然建议您保持简单,改用两个文本框.

I still advice that you keep it simple and use two textboxes instead.

这篇关于在MVC中使用两个实体模型列创建TextBoxfor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆