1

i want to display attribute with condition for example if the previous label(number) is 1 so the second(price) should be automatically 4

1-4;

2-5;

3-14,

 @Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
    @Html.LabelFor(model => model.number, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.number,listItems, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.number, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.price, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.price, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.price, "", new { @class = "text-danger" })
    </div>
</div>
2
  • 1
    where or how will decide value for Model.number? Commented Dec 29, 2019 at 2:57
  • you should use javascript for implementing this kind of problem. On the change event of number input, you should change the price input value. You can see an example here: stackoverflow.com/questions/8674878/… Commented Dec 29, 2019 at 6:43

1 Answer 1

1

You can use Jquery to handle event dropdown list of number changing.

It look like:

var $number = $("#number"); 
$number.on("change", function(){
         // do something here
});

And then, you assign value for price accordingly.

var number = parseInt($(this).val(), 10);

var priceAccordingTo = 0;
switch(number){
    case 1: priceAccordingTo = 4; break;
    case 2: priceAccordingTo = 5; break;
    case 3: priceAccordingTo = 14; break;
    }
$("#price").val(priceAccordingTo);

Here is the link for demo.

Sign up to request clarification or add additional context in comments.

6 Comments

thank you very much its work, please can you tell me if i had a string by what i should replace var number = parseInt($(this).val(), 10);
I don't quite get what you mean. @john smith . My answer is correct, isn't it?
yes thank you your answer is correct 100/100,but i have another question what if i have a string as a condition
If your condition is string instead of int type, You need only to do something like this var number = $(this).val(). No need to convert from string to int
Have you got that ? @john smith
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.