MVC: split string from model in multiple text boxesHow do you create a dropdownlist from an enum in ASP.NET MVC?How do you handle multiple submit buttons in ASP.NET MVC Framework?In MVC, how do I return a string result?Easiest way to split a string on newlines in .NET?C# Split A String By Another StringHow to control both the width and decimal places for a text box?ASP.NET MVC 5 Scaffolding not creating elements for enum propertyMultiple lines instead of single line in HtmlEditFor() function? ASP.NET MVC 5 RazorHow to use datetime picker as editortemplatecant set the size of a multi line text box mvc
Does the reader need to like the PoV character?
Review your own paper in Mathematics
Giving feedback to someone without sounding prejudiced
What (the heck) is a Super Worm Equinox Moon?
How to draw a matrix with arrows in limited space
How can ping know if my host is down
Which Article Helped Get Rid of Technobabble in RPGs?
What is the English pronunciation of "pain au chocolat"?
What is the difference between lands and mana?
Is this toilet slogan correct usage of the English language?
What fields between the rationals and the reals allow a good notion of 2D distance?
Is my low blitz game drawing rate at www.chess.com an indicator that I am weak in chess?
awk assign to multiple variables at once
US tourist/student visa
Find the next value of this number series
Why do ¬, ∀ and ∃ have the same precedence?
Creating two special characters
Doesn't the system of the Supreme Court oppose justice?
Quoting Keynes in a lecture
Taxes on Dividends in a Roth IRA
How do you make your own symbol when Detexify fails?
How to convince somebody that he is fit for something else, but not this job?
"It doesn't matter" or "it won't matter"?
Pre-mixing cryogenic fuels and using only one fuel tank
MVC: split string from model in multiple text boxes
How do you create a dropdownlist from an enum in ASP.NET MVC?How do you handle multiple submit buttons in ASP.NET MVC Framework?In MVC, how do I return a string result?Easiest way to split a string on newlines in .NET?C# Split A String By Another StringHow to control both the width and decimal places for a text box?ASP.NET MVC 5 Scaffolding not creating elements for enum propertyMultiple lines instead of single line in HtmlEditFor() function? ASP.NET MVC 5 RazorHow to use datetime picker as editortemplatecant set the size of a multi line text box mvc
I have a model:
public class Data
public int id get; set;
public string BBBCode get; set;
Part of view:
<div class="col-md-10">
@Html.EditorFor(model => model.BBBCode, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBCode, "", new @class = "text-danger" )
</div>
for this model, Visual studio created an edit view with text box for the BBBCode
member. But I need to split BBBCode
in two textboxes, first text box should have maximum 4 characters and the second text box should contain the rest of the characters. Is there a way to achieve this automatically or without any javascript code and without modifying the model?
c# asp.net-mvc razor
add a comment |
I have a model:
public class Data
public int id get; set;
public string BBBCode get; set;
Part of view:
<div class="col-md-10">
@Html.EditorFor(model => model.BBBCode, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBCode, "", new @class = "text-danger" )
</div>
for this model, Visual studio created an edit view with text box for the BBBCode
member. But I need to split BBBCode
in two textboxes, first text box should have maximum 4 characters and the second text box should contain the rest of the characters. Is there a way to achieve this automatically or without any javascript code and without modifying the model?
c# asp.net-mvc razor
1
have your tried custom editor template ?
– Suren Srapyan
Nov 20 '15 at 13:33
@SurenSrapyan No. Can you please send me a link or something? I'm new to MVC and I know only the basic stuff. Thanks
– Buda Gavril
Nov 20 '15 at 13:34
try this: codeguru.com/csharp/.net/net_asp/mvc/… and you can search for this like: MVC Editor Templates
– Suren Srapyan
Nov 20 '15 at 13:37
but as an advice I'll say you.Your View is wanted 2 textboxes for your model.But your model has only the one which must be changed.So it will be good if you make a ViewModel(about ViewModel search).In ViewModel there is everything that your View want.So in the VIewModel you can have 2 separate strings(BBBCode pieces) and give your View's Model to that ViewModel
– Suren Srapyan
Nov 20 '15 at 13:40
add a comment |
I have a model:
public class Data
public int id get; set;
public string BBBCode get; set;
Part of view:
<div class="col-md-10">
@Html.EditorFor(model => model.BBBCode, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBCode, "", new @class = "text-danger" )
</div>
for this model, Visual studio created an edit view with text box for the BBBCode
member. But I need to split BBBCode
in two textboxes, first text box should have maximum 4 characters and the second text box should contain the rest of the characters. Is there a way to achieve this automatically or without any javascript code and without modifying the model?
c# asp.net-mvc razor
I have a model:
public class Data
public int id get; set;
public string BBBCode get; set;
Part of view:
<div class="col-md-10">
@Html.EditorFor(model => model.BBBCode, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBCode, "", new @class = "text-danger" )
</div>
for this model, Visual studio created an edit view with text box for the BBBCode
member. But I need to split BBBCode
in two textboxes, first text box should have maximum 4 characters and the second text box should contain the rest of the characters. Is there a way to achieve this automatically or without any javascript code and without modifying the model?
c# asp.net-mvc razor
c# asp.net-mvc razor
asked Nov 20 '15 at 13:32
Buda GavrilBuda Gavril
11.4k30100151
11.4k30100151
1
have your tried custom editor template ?
– Suren Srapyan
Nov 20 '15 at 13:33
@SurenSrapyan No. Can you please send me a link or something? I'm new to MVC and I know only the basic stuff. Thanks
– Buda Gavril
Nov 20 '15 at 13:34
try this: codeguru.com/csharp/.net/net_asp/mvc/… and you can search for this like: MVC Editor Templates
– Suren Srapyan
Nov 20 '15 at 13:37
but as an advice I'll say you.Your View is wanted 2 textboxes for your model.But your model has only the one which must be changed.So it will be good if you make a ViewModel(about ViewModel search).In ViewModel there is everything that your View want.So in the VIewModel you can have 2 separate strings(BBBCode pieces) and give your View's Model to that ViewModel
– Suren Srapyan
Nov 20 '15 at 13:40
add a comment |
1
have your tried custom editor template ?
– Suren Srapyan
Nov 20 '15 at 13:33
@SurenSrapyan No. Can you please send me a link or something? I'm new to MVC and I know only the basic stuff. Thanks
– Buda Gavril
Nov 20 '15 at 13:34
try this: codeguru.com/csharp/.net/net_asp/mvc/… and you can search for this like: MVC Editor Templates
– Suren Srapyan
Nov 20 '15 at 13:37
but as an advice I'll say you.Your View is wanted 2 textboxes for your model.But your model has only the one which must be changed.So it will be good if you make a ViewModel(about ViewModel search).In ViewModel there is everything that your View want.So in the VIewModel you can have 2 separate strings(BBBCode pieces) and give your View's Model to that ViewModel
– Suren Srapyan
Nov 20 '15 at 13:40
1
1
have your tried custom editor template ?
– Suren Srapyan
Nov 20 '15 at 13:33
have your tried custom editor template ?
– Suren Srapyan
Nov 20 '15 at 13:33
@SurenSrapyan No. Can you please send me a link or something? I'm new to MVC and I know only the basic stuff. Thanks
– Buda Gavril
Nov 20 '15 at 13:34
@SurenSrapyan No. Can you please send me a link or something? I'm new to MVC and I know only the basic stuff. Thanks
– Buda Gavril
Nov 20 '15 at 13:34
try this: codeguru.com/csharp/.net/net_asp/mvc/… and you can search for this like: MVC Editor Templates
– Suren Srapyan
Nov 20 '15 at 13:37
try this: codeguru.com/csharp/.net/net_asp/mvc/… and you can search for this like: MVC Editor Templates
– Suren Srapyan
Nov 20 '15 at 13:37
but as an advice I'll say you.Your View is wanted 2 textboxes for your model.But your model has only the one which must be changed.So it will be good if you make a ViewModel(about ViewModel search).In ViewModel there is everything that your View want.So in the VIewModel you can have 2 separate strings(BBBCode pieces) and give your View's Model to that ViewModel
– Suren Srapyan
Nov 20 '15 at 13:40
but as an advice I'll say you.Your View is wanted 2 textboxes for your model.But your model has only the one which must be changed.So it will be good if you make a ViewModel(about ViewModel search).In ViewModel there is everything that your View want.So in the VIewModel you can have 2 separate strings(BBBCode pieces) and give your View's Model to that ViewModel
– Suren Srapyan
Nov 20 '15 at 13:40
add a comment |
4 Answers
4
active
oldest
votes
Ideally, your viewmodel should correspond to the form you actually render. Mostly if Data
belongs to business logic. I.e:
public class Data
public int id get; set;
public string BBBCode get; set;
public class DataViewModel
public DataViewModel()
public DataViewModel(Data data)
id = data.id ;
BBBCodePartOne = ... ;
BBBCodePartTwo = ... ;
public int id get; set;
public string BBBCodePartOne get; set;
public string BBBCodePartTwo get; set;
public Data ToData()
return new Data() id = id, BBBCode = BBBCodePartOne + BBBCodePartTwo ;
add a comment |
It's better to do it in your View model i mean like this:
public class Data
public static char Separator = '-'; //here is your separator
public int id get; set;
public string BBBBegin get; set;
public string BBBEnd get; set;
public string BBBCode get return BBBBegin + Separator + BBBEnd;
Now on your View you always can do it like this:
<div class="col-md-10">
@Html.EditorFor(model => model.BBBBegin, new htmlAttributes = new @class = "form-control" )
@Html.EditorFor(model => model.BBBEnd, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBBegin, "", new @class = "text-danger" )
@Html.ValidationMessageFor(model => model.BBBEnd, "", new @class = "text-danger" )
</div>
And when you post it to controller you always can get full BBBCode from BBBCode
property.
and when I will want to save the model, will it automaticaly populate BBBCode with the text from those two text boxes?
– Buda Gavril
Nov 20 '15 at 13:44
@BudaGavril you should just understand wht will happened. You post form with your textboxes, then MVC will bind values toBBBBegin
BBBEnd
properties. Then you can just get full code fromBBBCode
property. i suppose it's pretty clear how it will work
– teo van kot
Nov 20 '15 at 13:46
I understand that, but I've asked if there's a way to bind automatically those two text boxes to my property when the model is validated.
– Buda Gavril
Nov 20 '15 at 13:52
@BudaGavril yes there is. You should create yourCustomModelBinder
but it's really now wotrh it. It's better to extend existing model or create ViewModel like @Ksv3n advice you
– teo van kot
Nov 20 '15 at 13:54
add a comment |
You should use two property in your model.
public class Data
public int id get; set;
public string BBBCode1 get; set;
public string BBBCode2 get; set;
<div class="col-md-10">
@Html.EditorFor(model => model.BBBCode1, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBCode1, "", new @class = "text-danger" )
</div>
<div class="col-md-10">
@Html.EditorFor(model => model.BBBCode2, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBCode2, "", new @class = "text-danger" )
</div>
in your controller
public ActionResult Index(Data model)
var bbcCode = model.BBCCode1 + model.BBCCode2;
My model is an entity and it has only one member for this code and I want to be automatically populated when I edit the data.
– Buda Gavril
Nov 20 '15 at 13:45
you shouldn't use your entity as a view model. You should create a new view model class so you should use it in your view as model. After that populate data in your controller like my sample code.
– Yargicx
Nov 20 '15 at 14:11
add a comment |
If you don't want to change the Model (no judgement)
I used these two in my input field, one for both parts of the value:
<div>@Html.TextBoxFor(m => m.Betrag, new @Value = Model.Betrag.ToString().Split(',').GetValue(0) )</div>
<div>@Html.TextBoxFor(m => m.Betrag, new @Value = Model.Betrag.ToString().Split(',').GetValue(1) )</div>
This simply splits up a value like '15,00' into a '15' and '00', you can then join the values together again afterwards with some Javascript and JQuery. This works great for me.
New contributor
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33828004%2fmvc-split-string-from-model-in-multiple-text-boxes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Ideally, your viewmodel should correspond to the form you actually render. Mostly if Data
belongs to business logic. I.e:
public class Data
public int id get; set;
public string BBBCode get; set;
public class DataViewModel
public DataViewModel()
public DataViewModel(Data data)
id = data.id ;
BBBCodePartOne = ... ;
BBBCodePartTwo = ... ;
public int id get; set;
public string BBBCodePartOne get; set;
public string BBBCodePartTwo get; set;
public Data ToData()
return new Data() id = id, BBBCode = BBBCodePartOne + BBBCodePartTwo ;
add a comment |
Ideally, your viewmodel should correspond to the form you actually render. Mostly if Data
belongs to business logic. I.e:
public class Data
public int id get; set;
public string BBBCode get; set;
public class DataViewModel
public DataViewModel()
public DataViewModel(Data data)
id = data.id ;
BBBCodePartOne = ... ;
BBBCodePartTwo = ... ;
public int id get; set;
public string BBBCodePartOne get; set;
public string BBBCodePartTwo get; set;
public Data ToData()
return new Data() id = id, BBBCode = BBBCodePartOne + BBBCodePartTwo ;
add a comment |
Ideally, your viewmodel should correspond to the form you actually render. Mostly if Data
belongs to business logic. I.e:
public class Data
public int id get; set;
public string BBBCode get; set;
public class DataViewModel
public DataViewModel()
public DataViewModel(Data data)
id = data.id ;
BBBCodePartOne = ... ;
BBBCodePartTwo = ... ;
public int id get; set;
public string BBBCodePartOne get; set;
public string BBBCodePartTwo get; set;
public Data ToData()
return new Data() id = id, BBBCode = BBBCodePartOne + BBBCodePartTwo ;
Ideally, your viewmodel should correspond to the form you actually render. Mostly if Data
belongs to business logic. I.e:
public class Data
public int id get; set;
public string BBBCode get; set;
public class DataViewModel
public DataViewModel()
public DataViewModel(Data data)
id = data.id ;
BBBCodePartOne = ... ;
BBBCodePartTwo = ... ;
public int id get; set;
public string BBBCodePartOne get; set;
public string BBBCodePartTwo get; set;
public Data ToData()
return new Data() id = id, BBBCode = BBBCodePartOne + BBBCodePartTwo ;
edited Nov 20 '15 at 13:46
answered Nov 20 '15 at 13:41
Mr. FahrenheitMr. Fahrenheit
9,23421541
9,23421541
add a comment |
add a comment |
It's better to do it in your View model i mean like this:
public class Data
public static char Separator = '-'; //here is your separator
public int id get; set;
public string BBBBegin get; set;
public string BBBEnd get; set;
public string BBBCode get return BBBBegin + Separator + BBBEnd;
Now on your View you always can do it like this:
<div class="col-md-10">
@Html.EditorFor(model => model.BBBBegin, new htmlAttributes = new @class = "form-control" )
@Html.EditorFor(model => model.BBBEnd, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBBegin, "", new @class = "text-danger" )
@Html.ValidationMessageFor(model => model.BBBEnd, "", new @class = "text-danger" )
</div>
And when you post it to controller you always can get full BBBCode from BBBCode
property.
and when I will want to save the model, will it automaticaly populate BBBCode with the text from those two text boxes?
– Buda Gavril
Nov 20 '15 at 13:44
@BudaGavril you should just understand wht will happened. You post form with your textboxes, then MVC will bind values toBBBBegin
BBBEnd
properties. Then you can just get full code fromBBBCode
property. i suppose it's pretty clear how it will work
– teo van kot
Nov 20 '15 at 13:46
I understand that, but I've asked if there's a way to bind automatically those two text boxes to my property when the model is validated.
– Buda Gavril
Nov 20 '15 at 13:52
@BudaGavril yes there is. You should create yourCustomModelBinder
but it's really now wotrh it. It's better to extend existing model or create ViewModel like @Ksv3n advice you
– teo van kot
Nov 20 '15 at 13:54
add a comment |
It's better to do it in your View model i mean like this:
public class Data
public static char Separator = '-'; //here is your separator
public int id get; set;
public string BBBBegin get; set;
public string BBBEnd get; set;
public string BBBCode get return BBBBegin + Separator + BBBEnd;
Now on your View you always can do it like this:
<div class="col-md-10">
@Html.EditorFor(model => model.BBBBegin, new htmlAttributes = new @class = "form-control" )
@Html.EditorFor(model => model.BBBEnd, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBBegin, "", new @class = "text-danger" )
@Html.ValidationMessageFor(model => model.BBBEnd, "", new @class = "text-danger" )
</div>
And when you post it to controller you always can get full BBBCode from BBBCode
property.
and when I will want to save the model, will it automaticaly populate BBBCode with the text from those two text boxes?
– Buda Gavril
Nov 20 '15 at 13:44
@BudaGavril you should just understand wht will happened. You post form with your textboxes, then MVC will bind values toBBBBegin
BBBEnd
properties. Then you can just get full code fromBBBCode
property. i suppose it's pretty clear how it will work
– teo van kot
Nov 20 '15 at 13:46
I understand that, but I've asked if there's a way to bind automatically those two text boxes to my property when the model is validated.
– Buda Gavril
Nov 20 '15 at 13:52
@BudaGavril yes there is. You should create yourCustomModelBinder
but it's really now wotrh it. It's better to extend existing model or create ViewModel like @Ksv3n advice you
– teo van kot
Nov 20 '15 at 13:54
add a comment |
It's better to do it in your View model i mean like this:
public class Data
public static char Separator = '-'; //here is your separator
public int id get; set;
public string BBBBegin get; set;
public string BBBEnd get; set;
public string BBBCode get return BBBBegin + Separator + BBBEnd;
Now on your View you always can do it like this:
<div class="col-md-10">
@Html.EditorFor(model => model.BBBBegin, new htmlAttributes = new @class = "form-control" )
@Html.EditorFor(model => model.BBBEnd, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBBegin, "", new @class = "text-danger" )
@Html.ValidationMessageFor(model => model.BBBEnd, "", new @class = "text-danger" )
</div>
And when you post it to controller you always can get full BBBCode from BBBCode
property.
It's better to do it in your View model i mean like this:
public class Data
public static char Separator = '-'; //here is your separator
public int id get; set;
public string BBBBegin get; set;
public string BBBEnd get; set;
public string BBBCode get return BBBBegin + Separator + BBBEnd;
Now on your View you always can do it like this:
<div class="col-md-10">
@Html.EditorFor(model => model.BBBBegin, new htmlAttributes = new @class = "form-control" )
@Html.EditorFor(model => model.BBBEnd, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBBegin, "", new @class = "text-danger" )
@Html.ValidationMessageFor(model => model.BBBEnd, "", new @class = "text-danger" )
</div>
And when you post it to controller you always can get full BBBCode from BBBCode
property.
answered Nov 20 '15 at 13:41
teo van kotteo van kot
11.3k103357
11.3k103357
and when I will want to save the model, will it automaticaly populate BBBCode with the text from those two text boxes?
– Buda Gavril
Nov 20 '15 at 13:44
@BudaGavril you should just understand wht will happened. You post form with your textboxes, then MVC will bind values toBBBBegin
BBBEnd
properties. Then you can just get full code fromBBBCode
property. i suppose it's pretty clear how it will work
– teo van kot
Nov 20 '15 at 13:46
I understand that, but I've asked if there's a way to bind automatically those two text boxes to my property when the model is validated.
– Buda Gavril
Nov 20 '15 at 13:52
@BudaGavril yes there is. You should create yourCustomModelBinder
but it's really now wotrh it. It's better to extend existing model or create ViewModel like @Ksv3n advice you
– teo van kot
Nov 20 '15 at 13:54
add a comment |
and when I will want to save the model, will it automaticaly populate BBBCode with the text from those two text boxes?
– Buda Gavril
Nov 20 '15 at 13:44
@BudaGavril you should just understand wht will happened. You post form with your textboxes, then MVC will bind values toBBBBegin
BBBEnd
properties. Then you can just get full code fromBBBCode
property. i suppose it's pretty clear how it will work
– teo van kot
Nov 20 '15 at 13:46
I understand that, but I've asked if there's a way to bind automatically those two text boxes to my property when the model is validated.
– Buda Gavril
Nov 20 '15 at 13:52
@BudaGavril yes there is. You should create yourCustomModelBinder
but it's really now wotrh it. It's better to extend existing model or create ViewModel like @Ksv3n advice you
– teo van kot
Nov 20 '15 at 13:54
and when I will want to save the model, will it automaticaly populate BBBCode with the text from those two text boxes?
– Buda Gavril
Nov 20 '15 at 13:44
and when I will want to save the model, will it automaticaly populate BBBCode with the text from those two text boxes?
– Buda Gavril
Nov 20 '15 at 13:44
@BudaGavril you should just understand wht will happened. You post form with your textboxes, then MVC will bind values to
BBBBegin
BBBEnd
properties. Then you can just get full code from BBBCode
property. i suppose it's pretty clear how it will work– teo van kot
Nov 20 '15 at 13:46
@BudaGavril you should just understand wht will happened. You post form with your textboxes, then MVC will bind values to
BBBBegin
BBBEnd
properties. Then you can just get full code from BBBCode
property. i suppose it's pretty clear how it will work– teo van kot
Nov 20 '15 at 13:46
I understand that, but I've asked if there's a way to bind automatically those two text boxes to my property when the model is validated.
– Buda Gavril
Nov 20 '15 at 13:52
I understand that, but I've asked if there's a way to bind automatically those two text boxes to my property when the model is validated.
– Buda Gavril
Nov 20 '15 at 13:52
@BudaGavril yes there is. You should create your
CustomModelBinder
but it's really now wotrh it. It's better to extend existing model or create ViewModel like @Ksv3n advice you– teo van kot
Nov 20 '15 at 13:54
@BudaGavril yes there is. You should create your
CustomModelBinder
but it's really now wotrh it. It's better to extend existing model or create ViewModel like @Ksv3n advice you– teo van kot
Nov 20 '15 at 13:54
add a comment |
You should use two property in your model.
public class Data
public int id get; set;
public string BBBCode1 get; set;
public string BBBCode2 get; set;
<div class="col-md-10">
@Html.EditorFor(model => model.BBBCode1, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBCode1, "", new @class = "text-danger" )
</div>
<div class="col-md-10">
@Html.EditorFor(model => model.BBBCode2, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBCode2, "", new @class = "text-danger" )
</div>
in your controller
public ActionResult Index(Data model)
var bbcCode = model.BBCCode1 + model.BBCCode2;
My model is an entity and it has only one member for this code and I want to be automatically populated when I edit the data.
– Buda Gavril
Nov 20 '15 at 13:45
you shouldn't use your entity as a view model. You should create a new view model class so you should use it in your view as model. After that populate data in your controller like my sample code.
– Yargicx
Nov 20 '15 at 14:11
add a comment |
You should use two property in your model.
public class Data
public int id get; set;
public string BBBCode1 get; set;
public string BBBCode2 get; set;
<div class="col-md-10">
@Html.EditorFor(model => model.BBBCode1, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBCode1, "", new @class = "text-danger" )
</div>
<div class="col-md-10">
@Html.EditorFor(model => model.BBBCode2, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBCode2, "", new @class = "text-danger" )
</div>
in your controller
public ActionResult Index(Data model)
var bbcCode = model.BBCCode1 + model.BBCCode2;
My model is an entity and it has only one member for this code and I want to be automatically populated when I edit the data.
– Buda Gavril
Nov 20 '15 at 13:45
you shouldn't use your entity as a view model. You should create a new view model class so you should use it in your view as model. After that populate data in your controller like my sample code.
– Yargicx
Nov 20 '15 at 14:11
add a comment |
You should use two property in your model.
public class Data
public int id get; set;
public string BBBCode1 get; set;
public string BBBCode2 get; set;
<div class="col-md-10">
@Html.EditorFor(model => model.BBBCode1, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBCode1, "", new @class = "text-danger" )
</div>
<div class="col-md-10">
@Html.EditorFor(model => model.BBBCode2, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBCode2, "", new @class = "text-danger" )
</div>
in your controller
public ActionResult Index(Data model)
var bbcCode = model.BBCCode1 + model.BBCCode2;
You should use two property in your model.
public class Data
public int id get; set;
public string BBBCode1 get; set;
public string BBBCode2 get; set;
<div class="col-md-10">
@Html.EditorFor(model => model.BBBCode1, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBCode1, "", new @class = "text-danger" )
</div>
<div class="col-md-10">
@Html.EditorFor(model => model.BBBCode2, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.BBBCode2, "", new @class = "text-danger" )
</div>
in your controller
public ActionResult Index(Data model)
var bbcCode = model.BBCCode1 + model.BBCCode2;
answered Nov 20 '15 at 13:42
YargicxYargicx
5921723
5921723
My model is an entity and it has only one member for this code and I want to be automatically populated when I edit the data.
– Buda Gavril
Nov 20 '15 at 13:45
you shouldn't use your entity as a view model. You should create a new view model class so you should use it in your view as model. After that populate data in your controller like my sample code.
– Yargicx
Nov 20 '15 at 14:11
add a comment |
My model is an entity and it has only one member for this code and I want to be automatically populated when I edit the data.
– Buda Gavril
Nov 20 '15 at 13:45
you shouldn't use your entity as a view model. You should create a new view model class so you should use it in your view as model. After that populate data in your controller like my sample code.
– Yargicx
Nov 20 '15 at 14:11
My model is an entity and it has only one member for this code and I want to be automatically populated when I edit the data.
– Buda Gavril
Nov 20 '15 at 13:45
My model is an entity and it has only one member for this code and I want to be automatically populated when I edit the data.
– Buda Gavril
Nov 20 '15 at 13:45
you shouldn't use your entity as a view model. You should create a new view model class so you should use it in your view as model. After that populate data in your controller like my sample code.
– Yargicx
Nov 20 '15 at 14:11
you shouldn't use your entity as a view model. You should create a new view model class so you should use it in your view as model. After that populate data in your controller like my sample code.
– Yargicx
Nov 20 '15 at 14:11
add a comment |
If you don't want to change the Model (no judgement)
I used these two in my input field, one for both parts of the value:
<div>@Html.TextBoxFor(m => m.Betrag, new @Value = Model.Betrag.ToString().Split(',').GetValue(0) )</div>
<div>@Html.TextBoxFor(m => m.Betrag, new @Value = Model.Betrag.ToString().Split(',').GetValue(1) )</div>
This simply splits up a value like '15,00' into a '15' and '00', you can then join the values together again afterwards with some Javascript and JQuery. This works great for me.
New contributor
add a comment |
If you don't want to change the Model (no judgement)
I used these two in my input field, one for both parts of the value:
<div>@Html.TextBoxFor(m => m.Betrag, new @Value = Model.Betrag.ToString().Split(',').GetValue(0) )</div>
<div>@Html.TextBoxFor(m => m.Betrag, new @Value = Model.Betrag.ToString().Split(',').GetValue(1) )</div>
This simply splits up a value like '15,00' into a '15' and '00', you can then join the values together again afterwards with some Javascript and JQuery. This works great for me.
New contributor
add a comment |
If you don't want to change the Model (no judgement)
I used these two in my input field, one for both parts of the value:
<div>@Html.TextBoxFor(m => m.Betrag, new @Value = Model.Betrag.ToString().Split(',').GetValue(0) )</div>
<div>@Html.TextBoxFor(m => m.Betrag, new @Value = Model.Betrag.ToString().Split(',').GetValue(1) )</div>
This simply splits up a value like '15,00' into a '15' and '00', you can then join the values together again afterwards with some Javascript and JQuery. This works great for me.
New contributor
If you don't want to change the Model (no judgement)
I used these two in my input field, one for both parts of the value:
<div>@Html.TextBoxFor(m => m.Betrag, new @Value = Model.Betrag.ToString().Split(',').GetValue(0) )</div>
<div>@Html.TextBoxFor(m => m.Betrag, new @Value = Model.Betrag.ToString().Split(',').GetValue(1) )</div>
This simply splits up a value like '15,00' into a '15' and '00', you can then join the values together again afterwards with some Javascript and JQuery. This works great for me.
New contributor
edited 13 hours ago
adiga
11.4k62545
11.4k62545
New contributor
answered 13 hours ago
Bradley WeibelBradley Weibel
11
11
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33828004%2fmvc-split-string-from-model-in-multiple-text-boxes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
have your tried custom editor template ?
– Suren Srapyan
Nov 20 '15 at 13:33
@SurenSrapyan No. Can you please send me a link or something? I'm new to MVC and I know only the basic stuff. Thanks
– Buda Gavril
Nov 20 '15 at 13:34
try this: codeguru.com/csharp/.net/net_asp/mvc/… and you can search for this like: MVC Editor Templates
– Suren Srapyan
Nov 20 '15 at 13:37
but as an advice I'll say you.Your View is wanted 2 textboxes for your model.But your model has only the one which must be changed.So it will be good if you make a ViewModel(about ViewModel search).In ViewModel there is everything that your View want.So in the VIewModel you can have 2 separate strings(BBBCode pieces) and give your View's Model to that ViewModel
– Suren Srapyan
Nov 20 '15 at 13:40