Click here to Skip to main content
15,896,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 2 tabs. i am using asp.net mvc. with one tab i am able to insert data.but when i insert data from another tab table of first tab in backebd inserting null row

this is my view

Razor
@model MathuraApp.Models.CurrencyCreationModel
   


@{
   ViewBag.Title = "currency";
    Layout = "~/Views/Shared/_AccountLayout.cshtml";
}

<h2>currency</h2>

@*@using (Html.BeginForm())


    {*@
@Html.AntiForgeryToken()
      BEGIN INLINE TABS PORTLET
<div class="widget">
    <div class="widget-title">
        <h4><i class="icon-user"></i></h4>
    </div>
    <div class="widget-body">
        <div class="row-fluid">
            <div class="span8">
            </div>
        </div>
    </div>
</div>

<div class="tabbable custom-tab">
    <h4>CurrencyCreationModel</h4>
    <hr />
    <ul class="nav nav-tabs">
        <li class="active"><a href="#tab_1_1" data-toggle="tab">Currency Creation</a></li>
        <li><a href="#tab_1_2" data-toggle="tab">Currency Converter</a></li>

    </ul>
    <div class="tab-content">


        <div class="tab-pane  " id="tab_1_2">
            @using (Html.BeginForm("Currency", "CurrencyCreation",FormMethod.Post,new { }))
            {

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

                <div class="form-group">
                    @Html.LabelFor(model =&gt; model.ConvRate, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model =&gt; model.ConvRate, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model =&gt; model.ConvRate, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model =&gt; model.PrimaryCurrency, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model =&gt; model.PrimaryCurrency, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model =&gt; model.PrimaryCurrency, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        &lt;input type="submit" value="submit" class="btn btn-default" /&gt;
                    </div>
                </div>


                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10" style="color:green">
                        @ViewBag.Message

                    </div>
                </div>


            }

        </div>


        <div class="tab-pane active " id="tab_1_1">

            @using (Html.BeginForm("currency", "CurrencyCreation", FormMethod.Post, new { }))
            {
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                <div class="form-group">
                    @Html.LabelFor(model =&gt; model.Currency_Name, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model =&gt; model.Currency_Name, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model =&gt; model.Currency_Name, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model =&gt; model.CurrencySymbol, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model =&gt; model.CurrencySymbol, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model =&gt; model.CurrencySymbol, "", new { @class = "text-danger" })
                    </div>
                </div>

                    <div class="form-group">
                        @Html.LabelFor(model =&gt; model.Abbreviation, htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.EditorFor(model =&gt; model.Abbreviation, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model =&gt; model.Abbreviation, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model =&gt; model.Sub_Currency, htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.EditorFor(model =&gt; model.Sub_Currency, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model =&gt; model.Sub_Currency, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model =&gt; model.Decimal_Place, htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.EditorFor(model =&gt; model.Decimal_Place, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model =&gt; model.Decimal_Place, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model =&gt; model.Comments, htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.EditorFor(model =&gt; model.Comments, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model =&gt; model.Comments, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            &lt;input type="submit" value="submit" class="btn btn-default" /&gt;
                        </div>
                    </div>



                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10" style="color:green">
                            @ViewBag.Message

                        </div>
                    </div>


            }
        </div>



this is my controller


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MathuraApp.Repository;
using MathuraApp.Models;

namespace MathuraApp.Controllers
{
    public class CurrencyCreationController : Controller
    {
        // GET: CurrencyCreation

     
        public ActionResult currency()
        {
            return PartialView();
        }
      


        [HttpPost]
        public ActionResult currency(CurrencyCreationModel ccm)
        {
           try
           {
                if (ModelState.IsValid)
               {
                    CurrencyCreationRepository ccr = new CurrencyCreationRepository();
                  
                    if (ccr.Currency(ccm) )
                    {
                        ViewBag.Message = "Currency details added successfully";
                        
                    }
                    CurrencyConverterRepository ccr1 = new CurrencyConverterRepository();

               if (ccr1.Currency(ccm))
                    {
                        ViewBag.Message = "Currencyconverter details added successfully";
                    }
                }

                return PartialView(ccm);
            }
            catch
            {
                return PartialView(ccm);
            }

           

      
           


        }


        //public ActionResult Converter(CurrencyCreationModel ccm)
        //{
        //    try
        //    {
        //        if (ModelState.IsValid)
        //        {
        //            CurrencyCreationRepository ccro = new CurrencyCreationRepository();

        //            if (ccro.Converter(ccm))
        //            {
        //                ViewBag.Message = "Currency details added successfully";
        //            }
        //        }

        //        return View(ccm);
        //    }
        //    catch
        //    {
        //        return View(ccm);
        //    }
        //}


    }
}
Posted
Updated 30-Apr-16 4:44am
v2

1 solution

I can't read that pile of code you posted because it's not formatted and enclosed in PRE tags.

But, in the web world, your tabs are VISUALLY separate, not separate from each other as far as a form tag or the code is concerned. What it appears that you're doing is you have two forms, one nested inside the other. When you submit the form that you think is one one form, you're actually submitting both of them.

Your controller code also doesn't appear to check to see if the data that it's getting is valid. Sure, you check ModelState.IsValid, but that only goes so far to make sure certain fields are filled in. It has no way of knowing if the data in those fields is valid per your business logic.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900